l2aelba
l2aelba

Reputation: 22167

Set input:radio by jQuery then check checked by clicking

HTML :

<label><input type="radio" name="type">Stackoverflow</label>
<label><input type="radio" name="type">Google</label>

jQuery :

$("input:radio").eq(0).attr('checked',true); // Set 1st input checked by jQuery

$("input:radio:not(:checked)").on("click", function(){
      console.log("yes!");
});

Demo : http://jsbin.com/onayec/1/edit

Look like HTML attr checked="checked" doesn't remove when click another input:radio

I just want to check if not click input:radio who checked

Upvotes: 2

Views: 365

Answers (3)

Ram
Ram

Reputation: 144689

Events are bound to elements not to their properties, ie, when you select an element, the handler is bound to that element, the handler is not updated when properties are changed, if you want to bind a click handler to input elements that are not checked you should use event delegation.

However, it's not a good example for radio/checkbox input elements, what you are after is a simplechange event handler. The point was showing how events are bound to elements.

Upvotes: 1

palaѕн
palaѕн

Reputation: 73906

You can also try this:

$('input:radio').click(function () {
    if ($(this).is(':checked')) {
        // do something
        alert('Ckecked');
    }
});

Upvotes: 1

Adil
Adil

Reputation: 148130

I think using change event instead of click would fire the event when the radio button click is not already checked.

$("input:radio").on("change", function(){
      console.log("yes!");
});

Upvotes: 3

Related Questions