Search code examples
javascriptjqueryformsonchangefieldset

Fieldset .change jQuery


I'm trying to change a button value on triggering a radio button.

<fieldset id="product-color">
    <input type="radio" id="red" name="color" value="Red">
    <label for="red">Red</label><br> 
    <input type="radio" id="blue" name="color" value="Blue">
    <label for="blue">Blue</label><br> 
</fieldset>

<button
id="order-button"
data-item-id="1"
data-item-name="Shirt"
data-item-price="20"
data-item-custom2-name="Color"
data-item-custom2-options="Red|Blue">
Add Item
</button>

By using this small script:

$('#product-color').change(function() {
    $('#order-button').data('item-custom2-value', $(this).val());
});

With a select-input-field it works quite fine, but not with a fieldset. Is there any difference?


Solution

  • You need change event for radio button as event is fired on radio button and not fieldset:

    $('#product-color input').change(function() {
       $('#order-button').data('item-custom2-value', $(this).val());
    });
    

    Working Demo