Abdus Sattar Bhuiyan
Abdus Sattar Bhuiyan

Reputation: 3074

How to apply on change event on radio button using jquery

HTML:

<div class="radio-list  ">
     <label class="radio-inline">
     <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked=""></span> NEW INSTALLATION </label>
     <label class="radio-inline">
     <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="repair"></span> SERVICE REPAIR </label>
  <label class="radio-inline">
  <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel"></span> CANCEL SERVICE </label>
  <label class="radio-inline">
  <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="other"></span> OTHER SERVICE </label>
</div>

How to apply on change event on <input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel"> ?

Upvotes: 5

Views: 23677

Answers (4)

Allan
Allan

Reputation: 140

I've tried all the above codes, but for me what works is the below code when the radio buttons are generated dynamically.

$(document).ready(function () {
 
$(document).on("change","input[name='data[Customer][service_type]']" ,function() {
      console.log("radio changed");
  });
  });

Upvotes: 0

Sumit Kumar Gupta
Sumit Kumar Gupta

Reputation: 2364

I am using this code

<div class="radio-list  ">
     <label class="radio-inline">
     <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked="" onclick="myFunction(this.value)"></span> NEW INSTALLATION </label>

</div>

and javascript function code

function myFunction(myval)
{
alert(myval);
}

Upvotes: 0

Kishore Sahasranaman
Kishore Sahasranaman

Reputation: 4230

Use on change event .

$("[name='data[Customer][service_type]']").on("change", function (e) {
    console.log(this.value);
});
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="radio-list  ">
    <label class="radio-inline"> <span class=""><input type="radio" name="data[Customer][service_type]" id="service_type" value="new" checked=""></span> NEW INSTALLATION</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="repair"></span> SERVICE REPAIR</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="cancel"></span> CANCEL SERVICE</label>
    <label class="radio-inline"> <span class="checked"><input type="radio" name="data[Customer][service_type]" id="service_type" value="other"></span> OTHER SERVICE</label>
</div>

Upvotes: 1

void
void

Reputation: 36703

$(function(){

    $("input:radio[name='data[Customer][service_type]']").change(function(){
        var _val = $(this).val();
        console.log(_val);
    });

});

.change is the event which you need to trigger.

See it here.

Upvotes: 9

Related Questions