2

I am trying to change a select option on click but I don't want to use the option value. My code works if I give my button a value='3' but what I want is to select the one with data-price="0" which in my case is the one with value='3'.

JS :

jQuery(document).ready(function () {
    jQuery('#sample-addtocart-button').click(function(){
        jQuery('.product-custom-option').val(jQuery(this).attr('value'));
    });
});

Html :

<button value="0" id="sample-addtocart-button" type="button">Free</button>

<select class="product-custom-option">
    <option value="">-- Please Select --</option>
    <option data-price="10" value="1">£10.00</option>
    <option data-price="20" value="2">£20.00</option>
    <option data-price="0" value="3">FREE</option>
</select>

Any help will be appreciated

3 Answers 3

5

You can use attribute equals selector to get the option and then select option by setting selected property using prop() method.

jQuery(document).ready(function() {
  jQuery('#sample-addtocart-button').click(function() {
    jQuery('.product-custom-option option[data-price="' + jQuery(this).attr('value') + '"]').prop('selected', true);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button value="0" id="sample-addtocart-button" type="button">Free</button>

<select class="product-custom-option">
  <option value="">-- Please Select --</option>
  <option data-price="10" value="1">£10.00</option>
  <option data-price="20" value="2">£20.00</option>
  <option data-price="0" value="3">FREE</option>
</select>

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers mate, much appreciated!
1

You can select elements by attribute, and then set the selected property on the element.

jQuery(document).ready(function () {
    jQuery('#sample-addtocart-button').click(function(){
        jQuery('.product-custom-option [data-price=' + this.value + ']').prop("selected", true);
    });
});

This selects the element with a data-price attribute equal to the value of this.value, which is a descendant of .product-custom-option, and sets its selected property to true.


Without jQuery, it could look like this:

document.addEventListener("DOMContentLoaded", function () {
  document.querySelector('#sample-addtocart-button').addEventListener("click", function(){
    document.querySelector('.product-custom-option [data-price=' + this.value + ']').selected = true;
  });
});

And a handful of helper methods always helps with the verbosity:

function listen(el, typ, fn, cap) {
  el && el.addEventListener(typ, fn, cap)
}
function query(el, sel) {
  if (typeof el === "string") {
    sel = el;
    el = document;
  }
  return el.querySelector(sel)
}

listen(document, "DOMContentLoaded", function () {
  listen(query('#sample-addtocart-button'), "click", function(){
    query('.product-custom-option [data-price=' + this.value + ']').selected = true;
  });
});

2 Comments

Cheers mate, much appreciated!
My pleasure. Glad it helped.
0

Try this:

jQuery('#sample-addtocart-button').click(function(){
        var val= jQuery(this).attr('value');
        jQuery('.product-custom-option option[data-price='+val+']').prop('selected', true); // it will find the select option with certain data-price and make it selected
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.