views:

56

answers:

2

Hallo all. I got this piece of code:

<div>
    <input id="id1" name="radioButton" type="radio">
    <input type="text" id="idText1">
    <p></p>
    <input id="id2" name="radioButton" type="radio">
    <input type="text" id="idText2">

</div>

When I select the radio id1 I need to disable idText2 and when I select radio id2 I need to disable idText1. Is there an elegant way?

Kind regards Massimo

+1  A: 

I suppose that you want to enable the textbox next to the radio button also?

$(function(){
  $('#id1, #id2').click(function(){
    $('#idText1')[0].disabled = (this.id != 'id1');
    $('#idText2')[0].disabled = (this.id != 'id2');
  });
});

You might want to start with one radio button checked and the other textbox disabled:

<div>
  <input id="id1" name="radioButton" type="radio" checked>
  <input type="text" id="idText1">
  <p></p>
  <input id="id2" name="radioButton" type="radio">
  <input type="text" id="idText2" disabled>
</div>
Guffa
Why do ` $('#idText1')[0]...` if it's an ID selector? Surely there's only one.
Ben Shelock
@Ben, to get to the actual dom element, and not he jQuery object .. ( *because he is accessing the disabled property directly, not through the jquery setters..* )
Gaby
+3  A: 

and a one liner for some fun :) ( well besides the wrapping.. )

$('div :radio').click(function(){
   $(this).siblings(':text').attr('disabled','disabled').end().next(':text').removeAttr('disabled');
})
Gaby