Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing text of radio button

Tags:

jquery

I have a list of radio buttons like this:

<div id="TheOptions">
  <input type="radio" id="test1" name="mylist"/>option 1
  <input type="radio" id="test2" name="mylist"/>option 2
  <input type="radio" id="test3" name="mylist"/>option 3
  <input type="radio" id="test4" name="mylist"/>option 4
</div>

When I write

  $('#test1').html('best option');

The text is added to the radio button and the current text "option 1" still shows.

How can I change this to make it work?

Thanks.

like image 461
frenchie Avatar asked Apr 26 '11 19:04

frenchie


People also ask

How do I change the text on a button?

To change the button text, first we need to access the button element inside the JavaScript by using the document. getElementById() method and add a click event handler to the button, then set it's value property to blue . Now, when we click on our button , it changes the value from Red to Blue or vice versa.

How do I make radio buttons with text?

To label a radio button, add a <label> element after the <input> element and insert a for attribute with the same value as the id of the associated <input> element. Then, write your label text in the <label> tag.

Can radio buttons have different names?

Each check box must have a unique name. Radio buttons allow only one choice within a group of buttons. Each radio button within a group should have the same name. You can create more than one group of radio buttons by using different names.


1 Answers

You should wrap your text in <label> elements:

<div id="TheOptions">
  <input type="radio" id="test1" name="mylist"/><label for="test1">option 1</label>
  <input type="radio" id="test2" name="mylist"/><label for="test1">option 2</label>
  <input type="radio" id="test3" name="mylist"/><label for="test1">option 3</label>
  <input type="radio" id="test4" name="mylist"/><label for="test1">option 4</label>
</div>

Now your text is explicitly associated with the corresponding radio button and users will even be able to click on the text, visually impaired users will also be quite thankful for the improved usability. Then you can just do:

$('label[for=test1]').html('best option');
like image 160
mu is too short Avatar answered Sep 27 '22 19:09

mu is too short