2

I need some help please This is the html

<div>
<p>match1</p>
teamA <input type="radio" name="match1" onclick="update('ab');" />
teamB <input type="radio" name="match1" onclick="update('ba');" />
<p>match2</p>
teamC <input type="radio" name="match1" onclick="update('ad');" />
teamD <input type="radio" name="match1" onclick="update('dc');" />
</div>


<script>
update(results){.................}
</script>

I have this html so what I want I to know is
How can I disable the radio button once the user clicks on it
because the update() function changes the values when user clicks on radio button
if he keeps clicking like that then values change each time he clicks
or if he clicks on sibling radio button
so please can anyone tell me how to disable radio button
once user clicks on it
like for instance in match1
if user selects teamA radio button then i want to disable both teamA and teamB radio buttons
same for match2 if he clicks then disable the clciked radio and sibling radio aswell

Thanks for reading this can anyone help me please
I can use plain js, jquery or libraries

Jonas
  • 121,568
  • 97
  • 310
  • 388
user735205
  • 25
  • 1
  • 1
  • 4

5 Answers5

1

Use this:

$(":radio").click(function(){
   var radioName = $(this).attr("name"); //Get radio name
   $(":radio[name='"+radioName+"']").attr("disabled", true); //Disable all with the same name
});

What we're doing, is, when a user clicks a radio button, disable all radios with the same name.

Hope this helps. Cheers.

Edgar Villegas Alvarado
  • 18,204
  • 2
  • 42
  • 61
  • Getting the name attribute and doing another selection is kind of redundant isn't it? Why not `$(this).attr("disabled",true);`? (Or `.attr("disabled","disabled")`) – nnnnnn May 05 '11 at 03:47
  • @nnn, it's not redundant, cause the second selector will return many objects (radio names are not unique) – Edgar Villegas Alvarado May 05 '11 at 07:02
  • @Edgar - you're right, sorry, I missed that the point was to disable the whole group of radios. – nnnnnn May 06 '11 at 02:25
1

jsfiddle example

http://jsfiddle.net/nhZXg/

code

$(":radio").click(function(){
  $(this).attr('disabled','disabled');
});

this is the general way of doing it.

jQuery("input:radio").attr('disabled',true);

or

  jQuery("input:radio").attr('disabled','disabled');
kobe
  • 15,671
  • 15
  • 64
  • 91
1

Using Javascript, you can use a solution like this:

    document.getElementById("IDOfButtonElement").onclick = function(){
        document.getElementById("IDOfButtonElement").disabled=true;
    }
Jesufer Vn
  • 13,200
  • 6
  • 20
  • 26
0

Try this:

this.setAttribute('disabled','disabled');

I suggest you to add label tag around your inputs. It cause when user clicks on the text radio button changes.

<label>teamA <input type="radio" name="match1" onclick="update('ab');" /></label>
Mohsen
  • 64,437
  • 34
  • 159
  • 186
0
onclick="update('ad', this);"




function onclick(word, element){
    $(element).attr('disabled', 'disabled');
 }
Atticus
  • 6,585
  • 10
  • 35
  • 57
  • No prob -- this is more so to disable the one that was clicked, not sure if you were looking for that single one or the entire collection, otherwise I agree with the others on doing ":radio" selectors – Atticus May 05 '11 at 08:24