How can we run the function when I clicked the radio button in jQuery

I have three switches, and when I click on one switch, I want to have some functions using jQuery.

+3
source share
1 answer

This will run the code when you press (for example) a second radio station. Is that what you meant?

Example: http://jsfiddle.net/z2f5j/

HTML

<input type='radio' name='somename' />one
<input type='radio' name='somename' />two
<input type='radio' name='somename' />​three​​​

JQuery

    // :eq() uses a 0 based index, so 1 is the 2nd radio
$(':radio[name=somename]:eq(1)').click(function() {
    alert('i was clicked');
});​

You can also use an event changeinstead click.

Example: http://jsfiddle.net/z2f5j/2/

      // run code when the radio is selected
$(':radio[name=somename]:eq(1)').change(function() {
    alert('i was changed');
});​
+5
source

Source: https://habr.com/ru/post/1764660/


All Articles