views:

3811

answers:

8

In HTML, I have the following radio button choices

o Choice 1
o Choice 2
o Choice 3

What I would like to do is when someone SELECTs a radio button from above, that the text corresponding to that radio button selected becomes bold.

So, for example - if the person selects "Choice 2", the radio button selection would now look like:

o Choice 1
o Choice 2
o Choice 3

How do I do this? I assume either JavaScript or CSS has to be used.

Thanks in advance

UPDATE: How would you implement "nickf" solution without using jQuery?

+1  A: 

This might help you get started: Styling radio buttons with CSS

Alex Reynolds
+2  A: 

Use the OnChange event of your select to change the css style of the chosen element to bold.

Assaf Lavie
You can't use that to change the style of the button itself - only the textual label can be changed like that. If you look at his question, he seems to want the little circle bold as well.
Dominic Rodger
A: 
<input type=button onClick="this.style.fontWeight=bold">

This might work, I didn't test by myself. Of course there are much nicer solutions.

If you had a JavaScript framework (like jQuery) inserted in your code, it probably has many function available to do the same. Selectors, stylers, css-handlers and many more.

I'd rather suggest creating a CSS class like this:

.felkover
{
    font-weight: bold;
}

Then simply add or remove this definition in the class attribute of any given button.

pestaa
It's possible to select by keyboard. Use the onChange event instead and inspect the 'this.checked' property.
spoulson
Also, the first solution will just leave anything you click as bold, rather than unbolding previous selections like it should.
Kev
Correct. I didn't think of onChange. nickf has a better implementation above.
pestaa
onchange is not reliable on radio buttons, but onclick is fired even on non-mouse activation. Also of course ‘this.style.fontWeight’ will do nothing on the radio button itself, it is the label that's important.
bobince
+1  A: 

You could try doing everything with CSS.

I tried this and it works1:

<input type="radio" name="choice" value="1" checked /><span>First choice</span>
<input type="radio" name="choice" value="2" /><span>Second choice</span>

input[type="radio"]:checked + span
{
    font-weight: bold;
}

Explanation: This catches all spans following a radio input (type="radio") and is checked.

1 I tested with Firefox 3 and IE7 and it only worked with Firefox. (compatibility reference)

GoodEnough
the input element itself doesn't have any text to make bold.
nickf
@nickf right, fixed that
GoodEnough
I changed it to make it work, but it only works in Firefox.
GoodEnough
+3  A: 

I'd do something like this:

<label><input type="radio" name="myRadio" value="1" /> One</label>
<label><input type="radio" name="myRadio" value="2" /> Two</label>
<label><input type="radio" name="myRadio" value="3" /> Three</label>

With an onchange handler on each element to change the CSS class of the parent label. In jQuery it would look like this:

$(':radio').change(function() {           // get all the radio buttons and
                                          // add an onchange handler
    var $label = $(this).parent('label'); // get a reference to the parent label
    if (this.checked) {                   // if the radio is on...
        $label.addClass('selected');      // add the CSS class "selected" to the label
    } else {                              // otherwise...
        $label.removeClass('selected');   // take the class away.
    }
});

Just remember to override the default style for labels (which is bold):

label {
    font-weight: normal;
}
label.selected {
    font-weight: bold;
}
nickf
Despite submitting a different answer, I think this is how it should be done.
defrex
How would you do this without using jQuery???
Using an onChange event, and checking for the whether the radio button is selected.
Dominic Rodger
It would be onclick, for radio buttons.
bobince
What's the code? B/c things like $(':radio') appear to be jQuery specific.
yes, the sample I posted was using jQuery, though it should be legible enough as pseudo-code for it to be rebuilt using plain javascript. I'll comment it to make it easier to read.
nickf
A: 

amusing your buttons have labels around them to include the text, I would add this to the labels.

<label onchange="this.parent.children.fontWeight='normal';this.fontWeight='bold';">

Though ideally I'd not put this code inline, but hook it to them from somewhere in the head after the page has loaded.

defrex
There's no such event as HTMLLabelElement.onchange.
bobince
lol, that is completely true. I guess I wasn't thinking.
defrex
+6  A: 

You can do this purely with CSS, but you need to wrap the text of each input in a span or other tag:

<input type="radio" name="group1" value="Milk"><span>Milk</span><br>

Then just use the sibling selector:

input[type="radio"]:checked+span { font-weight: bold; }
Kev
BTW, I tested this just now and it works at least in FF2.
Kev
According to http://www.quirksmode.org/css/contents.html it probably won't do anything (i.e., will 'degrade gracefully' :) ) in IE6, but other browsers should handle it fine.
Kev
wow, exactly like my answer
GoodEnough
Despite not being totally cross-browser, this is a great solution.
nickf
Crossbrowser, I posted this while yours still said input[type="radio"][checked] and before you added span tags...
Kev
@Kev, maybe anyway at least the good answer is on top
GoodEnough
A: 

Here is an example :

<script>
  function makeBold()
  {
    var radios = document.getElementsByName("sex");
    for (var index = 0; index < radios.length; index++)
    {
      if (radios[index].checked)
      {
        document.getElementById("span_" + radios[index].value).style.fontWeight='bold';
      }
      else
      {
        document.getElementById("span_" + radios[index].value).style.fontWeight='normal';
      }
    }
  }
</script>

<input type="radio" name="sex" value="male" onclick="makeBold();"> 
<span id="span_male">Male</span>

<br>
<input type="radio" name="sex" value="female" onclick="makeBold()"> 
<span id="span_female">Female</span>

EDIT : You should define your text spans with ids like that : span_

Canavar
Does this unbold selected radio button text in the event I change my answer choice?
yes, copy it in a HTML file and try :)
Canavar