Justin Meltzer
Justin Meltzer

Reputation: 13558

Trigger some jQuery code when a radio button is selected

I have these two radio buttons:

Original <input id="video_is_derivative_false" name="video[is_derivative]" type="radio" value="false">
Derivative (<i>ex. remix, mashup etc...</i>) <input id="video_is_derivative_true" name="video[is_derivative]" type="radio" value="true">

and I want to call some jQuery code when the "Derivative" button is selected. How can I do this?

Upvotes: 4

Views: 7992

Answers (5)

Ender
Ender

Reputation: 15221

You will want to monitor the change event, then in the handler, check to make sure that the button is checked. The second part is important because the change event will also fire when it becomes unchecked. The code would look something like this:

$('#video_is_derivative_true').change(function() {
    if (this.checked) {
        alert('derivative checked!');
    }
});

Here's a live demo ->

Upvotes: 1

Vivek
Vivek

Reputation: 11028

$("#video_is_derivative_true").click(function(){ alert("your code goes here"); }); 

Upvotes: 0

digitalbath
digitalbath

Reputation: 7354

$('#video_is_derivative_true').bind('click change', function() {
    if (this.checked) {
        // derivative is checked
    }
});

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94489

$("#video_is_derivative_true").click(function(){
alert("your code goes here");
});

Add an onclick handler to the input tag

You could also put something on the change handler

    $("#video_is_derivative_true").change(function(){
    if($(this).is(':checked')){
            alert("more code here");
        }

    });

Upvotes: 3

Niklas
Niklas

Reputation: 30012

Just attach a change event to it:

$('#video_is_derivative_true').change(function(){
 console.log("Selected");   
})

example: http://jsfiddle.net/niklasvh/cyADB/

Upvotes: 3

Related Questions