Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript jquery radio button click

I have 2 radio buttons and jquery running.

<input type="radio" name="lom" value="1" checked> first <input type="radio" name="lom" value="2"> second 

Now, with a button I can set onClick to run a function. What is the way to make radio buttons run a function when I click on one of them?

like image 682
David19801 Avatar asked Feb 28 '11 12:02

David19801


2 Answers

You can use .change for what you want

$("input[@name='lom']").change(function(){     // Do something interesting here }); 

as of jQuery 1.3

you no longer need the '@'. Correct way to select is:

$("input[name='lom']") 
like image 104
Peter Kelly Avatar answered Sep 28 '22 06:09

Peter Kelly


If you have your radios in a container with id = radioButtonContainerId you can still use onClick and then check which one is selected and accordingly run some functions:

$('#radioButtonContainerId input:radio').click(function() {     if ($(this).val() === '1') {       myFunction();     } else if ($(this).val() === '2') {       myOtherFunction();     }    }); 
like image 36
JohnIdol Avatar answered Sep 28 '22 05:09

JohnIdol