3

How can i disable the one radio input from the radio group ?

<input type="radio" name="radiogrp" value="op1" checked="checked">Option1
<input type="radio" name="radiogrp" value="op2"> Option2
<input type="radio" name="radiogrp" value="op3" > Option3
<input type="radio" name="radiogrp" value="op4"> Option4

My question is i want to disable option1 after clicking on any other button

For example:

when i select option2, than option1 should be disabled

Starx
  • 77,474
  • 47
  • 185
  • 261
IT ppl
  • 2,626
  • 1
  • 39
  • 56
  • Look at the [updated answer](http://stackoverflow.com/a/9834948/601179) I added a cool way for it. – gdoron Mar 23 '12 at 08:10

6 Answers6

6

Check this Fiddle I have just added

Let me know if this is not what you intended

As requested - posted fiddle answer

$('.rd').click(function(){
    $('.rd[value="op1"]').attr('disabled', 'disabled');       
});​
Jibi Abraham
  • 4,636
  • 2
  • 31
  • 60
1

I am using jquery mobile with fancy dynamic styling, and I found that after performing the disable, I needed to explicitly issue an refresh using the checkboxradio("refresh") method call so that the styling would reflect the change. You can chain the method call, though, so it might look like this:

$('.rd').click(function(){
  $('.rd[value="op1"]').attr('disabled', 'disabled').checkboxradio("refresh");
});​

http://view.jquerymobile.com/1.3.2/dist/demos/faq/updating-the-value-of-enhanced-form-elements-does-not-work.html

While I appreciate gdoron's deeper, more technical, more efficient, more comprehensive approach, I believe that sometimes (just sometimes) the easier reading and shorter coding of Jibi Abraham's approach is warranted, especially in an otherwise lightweight situation.

I realize this isn't a distinct answer, but I don't have enough reputation to do a comment yet.

Justin
  • 397
  • 7
  • 17
1
$(":radio").change(function(){

    $(":radio[name='radiogrp']").slice(0,1).attr("disabled","disabled");
});

DEMO

Rafay
  • 30,950
  • 5
  • 68
  • 101
1
var radios = $('input[type="radio"][name="radiogrp"]');
radios.change(function() {
    if (this.value != "op1") {
        radios.filter('[value="op1"]').prop('disabled', true);
    }
});​

I cached the radio buttons, so I don't need to Query the DOM twice.

DEMO


Since, after the change, there is no way back, this is more fun way:

var radios = $('input[type="radio"][name="radiogrp"]');
var first = $('input[type="radio"][name="radiogrp"][value="op1"]');
radios.not(first).change(function() {
    alert('changed'); // getting called only once.
    first.prop('disabled', true);
    radios.unbind('change');
});​

LIVE DEMO

gdoron
  • 147,333
  • 58
  • 291
  • 367
1

Its very easy, use jQuery's attribute selector. Here is an example of disabling the radio button with value op3

$('input[value="op3"]').attr('disabled', 'disabled');

Demo


Here is your solution

var radios =  $('input[name=radiogrp]'); //Cache all the radio button
radios.click(function() { 
//Attach a function to the click event of all radio button

    if(this.value!='op1') { //Check if currently click button has value of op1
        radios.filter('[value="op1"]').prop('disabled', true); //if not disable the first
    }
});

Demo

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Starx
  • 77,474
  • 47
  • 185
  • 261
1

Try

$('input[value="op2"]').click(function(e)
     {
         $('input[value="op1"]').attr('disabled', 'disabled');
     });

http://jsfiddle.net/3CdZU/

Milan Halada
  • 1,943
  • 18
  • 28