Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide() radio button *and* its text label in jquery

I am going back over a recent project sorting out accessibility issues and was making sure all form elements had labels. Putting the label text into a tag caused a problem with some kludgy code I had written before.

Basically, if you have a radio button and its label:

<label for="zone_r1"><input type="radio" name="zone" id="zone_r1" value="NY" />New York</label>

And you use jquery to hide it like so:

$('#zone_r1').hide();

The actual button is hidden but not the label text. Originally I made a span for the label text and hid that like so:

<input id="NY" type="radio" name="zone" value="NY" /><span id="nyTXT">New York</span>

and

$('#NY').hide();
$('#nyTXT').hide();

Any ideas? I prefer not to use the kludge and it may not validate with the span in the label, but maybe I am being overly zealous.

like image 990
edzillion Avatar asked Mar 13 '09 17:03

edzillion


People also ask

How to hide radio buttons in jQuery?

Answer: Use the jQuery show() and hide() methods You can simply use the jQuery show() and hide() methods to show and hide the div elements based on the selection of radio buttons.

How do you keep a radio button and label on the same line?

As a quick solution either you can apply colspan for your td or You can have both the radio button controls in same td so that the change due to long text wont affect the display of radiobutton.

How do I select a radio button when a label is clicked?

To select a radio button by clicking on its text in React:Add a label element for each radio button. The htmlFor prop of each label should be set to the id of each radio button. Click on the label element to select the radio button.


1 Answers

$('#zone_r1').parent().hide();

works for me

like image 188
cobbal Avatar answered Jan 23 '23 12:01

cobbal