views:

5763

answers:

2

hi,

in my jsp i have a radiobutton group and a textbox (which is disabled initially) .

whenever user clicks on last(or, one of the) radio button the textbox should be enabled and when user clicks on some other radiobutton text box should again get disabled.

i am able to enable the initially disabled checkbox with the following code :

$("#DevGroup_OTHER").click(function(){
           $("#otherDevText").attr("disabled","");
          })

then how to do disable it again?

it could be a simpler one, but i am new to jquery hence searching around for a simpler one.

any help?

regards

+3  A: 
$("#some_other_radiobutton").click(function(){
  $("#otherDevText").attr("disabled","disabled");
});
Andy Gaskell
yeah, but now i have to write this block for all other radiobuttons
sangram
Why don't you just put a class on all the other radio buttons "disables-text" and hook up the click handler to that class? Just a thought, I'm a jQuery novice.
Larry Lustig
that's a better idea, thank u. any other simpler ways?
sangram
Larry is right. If you need to group radio buttons together use classes or put them in a common container (div for example).
Andy Gaskell
+2  A: 

Always disable it (for every radio button), then re-enable it if the radio button is the one that enables the textbox. Unless the user is on a machine built in 1980, it will be so fast, not one will ever know.

$('radio').click(function() { 
    $("#otherDevText").attr("disabled","disabled");
    if($(this).attr('id') == 'enable_textbox') {
        $("#otherDevText").attr("disabled","");
    }
});

Alternatively, if there are multiple radio buttons that will enable the textbox:

$('radio').click(function() { 
    $("#otherDevText").attr("disabled","disabled");
    if($(this).hasClass('enable_textbox')) {
        $("#otherDevText").attr("disabled","");
    }
});

Make sense?

KyleFarris
thank u farris, that's even better.another problem for me is i am using spring so instead of <input type="radiobutton">i used <form:radiobutton ....>any ideas on selecting all the radio buttons.thanks again.
sangram
yes i found one, selector by tag name.also, what will be there when we say $(this) in your code? is it the first selector ($('radio')) or second one ($("#otherDevText"))i am getting some command object name coz i use spring MVC!
sangram
I'm not familiar with Spring MVC, but I'm quite sure that by the jquery has a chance to interact with the code, the element *will* be `<input type="radiobutton">`. The answer to your second question: `$(this)` will be referencing the clicked `$('radio')` element.
KyleFarris
thank u, yes it is referring to the radiobutton clicked.but in my case "id" attribute is returning some wired result probably because spring MVC tag.any way, thanx for quick reply!
sangram