I have a form element like this:
<div id="myformelement">
<input type="radio" id="option1">
<label for="option2">Option 1</label>
<input type="radio" id="option2">
<label for="option2">Option 2</label>
<input type="radio" id="option3">
<label for="option3">Option 3</label>
<input type="radio" id="option4">
<label for="option4">Option 4</label>
</div>
I want to hide the input fields “option2” and “option3” and their labels.
I can hide the input bullets by addressing the id. Unfortunately the corresponding labels to the input fields only have a “for” tag with the id in it.
How can I do this with javascript (no jquery).
I found this question (Find html label associated with a given input), but this seems only to work with one label within an ID, I can not use this.
Thanks in advance!
Kind regards,
Malte
You can do it with
nextSibling:But I have a better option for you: It seems to be a well-kept secret that
labelelements can contain theinputthey relate to, and when they do noforis required at all. So if you change your HTML to:…it gets a lot easier:
You just find the
input, traverse up to its parent which is thelabel, and hide that (which will hide theinputas well).