21

I've got the following code to trigger a click event on some radio buttons! but it doesn't get fired! can any one help me with this!

CODE :

$("#inline_content input[name='type']").click(function(){
    if($('input:radio[name=type]:checked').val() == "walk_in"){
        $('#select-table > .roomNumber').attr('enabled',false);
    }
}); 

RADIO BUTTONS

<form class="type">
<input type="radio" name="type" checked="checked" value="guest">In House</input>
<input type="radio" name="type" value="walk_in">Walk In</input>
</form>.

Update

Tried onChange() too but not working.

4
  • 1
    OnChange isn't a thing, it's change. Also, why not use focus? Commented Jun 19, 2013 at 8:20
  • The event handler gets fired for me: jsfiddle.net/6UZKN, even with the if statement: jsfiddle.net/6UZKN/1. Commented Jun 19, 2013 at 8:23
  • 2
    use removeAttr('enabled') to be sure. Not all browsers support enabled="false".
    – Broxzier
    Commented Jun 19, 2013 at 8:27
  • @Broxzier: Good point. The problem seems not to be the handler, but the use of .attr. .removeAttr would work, but .prop would work fine as well. Commented Jun 19, 2013 at 8:30

6 Answers 6

40

It fires. Check demo http://jsfiddle.net/yeyene/kbAk3/

$("#inline_content input[name='type']").click(function(){
    alert('You clicked radio!');
    if($('input:radio[name=type]:checked').val() == "walk_in"){
        alert($('input:radio[name=type]:checked').val());
        //$('#select-table > .roomNumber').attr('enabled',false);
    }
});
1
  • Correct </input>, your code html in jsfiddle.net?
    – WMomesso
    Commented Oct 27, 2017 at 13:05
18

There are a couple of things wrong in this code:

  1. You're using <input> the wrong way. You should use a <label> if you want to make the text behind it clickable.
  2. It's setting the enabled attribute, which does not exist. Use disabled instead.
  3. If it would be an attribute, it's value should not be false, use disabled="disabled" or simply disabled without a value.
  4. If checking for someone clicking on a form event that will CHANGE it's value (like check-boxes and radio-buttons), use .change() instead.

I'm not sure what your code is supposed to do. My guess is that you want to disable the input field with class roomNumber once someone selects "Walk in" (and possibly re-enable when deselected). If so, try this code:

HTML:

<form class="type">
    <p>
        <input type="radio" name="type" checked="checked" id="guest" value="guest" />
        <label for="guest">In House</label>
    </p>
    <p>
        <input type="radio" name="type" id="walk_in" value="walk_in" />
        <label for="walk_in">Walk in</label>
    </p>
    <p>
        <input type="text" name="roomnumber" class="roomNumber" value="12345" />
    </p>
</form>

Javascript:

$("form input:radio").change(function () {
    if ($(this).val() == "walk_in") {
        // Disable your roomnumber element here
        $('.roomNumber').attr('disabled', 'disabled');
    } else {
        // Re-enable here I guess
        $('.roomNumber').removeAttr('disabled');
    }
});

I created a fiddle here: http://jsfiddle.net/k28xd/1/

5

Personally, for me, the best solution for a similar issue was:

HTML

<input type="radio" name="selectAll" value="true" />
<input type="radio" name="selectAll" value="false" />

JQuery

var $selectAll = $( "input:radio[name=selectAll]" );
    $selectAll.on( "change", function() {
         console.log( "selectAll: " + $(this).val() );
         // or
         alert( "selectAll: " + $(this).val() );
    });

*The event "click" can work in place of "change" as well.

Hope this helps!

1
  • Best answer so far
    – cwhisperer
    Commented May 16, 2021 at 14:20
1

A different way

$("#inline_content input[name='type']").change(function () {
    if ($(this).val() == "walk_in" && $(this).is(":checked")) {
        $('#select-table > .roomNumber').attr('enabled', false);
    }
});

Demo - http://jsfiddle.net/cB6xV/

2
  • You could simplify the body of the event handler to $('#select-table > .roomNumber').prop('enabled', this.value !== 'walk_in');. And better use .prop for boolean attributes. Commented Jun 19, 2013 at 8:28
  • Why didn't I think of that
    – Atif
    Commented Jun 19, 2013 at 8:29
0

Seems like you're #inline_content isn't there! Remove the jQuery-Selector or check the parent elements, maybe you have a typo or forgot to add the id.

(made you a jsfiddle, works after adding a parent <div id="inline_content">: http://jsfiddle.net/J5HdN/)

0

put ur js code under the form html or use $(document).ready(function(){}) and try this.

$('#inline_content input[type="radio"]').click(function(){
                if($(this).val() == "walk_in"){
                    alert('ok');
                }
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.