I have a challenge to use existing JavaScript with my HTML. My HTML are two radiobuttons and the element ‘textarea’. When one radiobutton is selected (button: No) the textarea has to be shown and when the other (button: Yes) is selected it has to be hidden.
<span><input type="radio" name="feedback" id="Yes" value="Yes"><label for="Yes">Yes</label></span>
<span><input type="radio" name="feedback" id="No" value="No"><label for="No">No</label></span>
<div class="">
<h5>lorem ipsum da lore</h5>
<textarea>Lorem ipsum da lore</textarea>
</div>
The following JavaScript I have to use to accomplish the desired effect. I am Fairly new to JavaScript, so I hope somebody can explain what the best way is with the JavaScript code hereafter:
$("input.kpn-bh-hideShow").change(function(){
var id1 = $(this).attr('class').split(' ')[1];
var id2 = $(this).attr('class').split(' ')[2];
if(id1 && id2){
$('#' + id1)
.hide()
.find('input, select')
.attr("disabled","disabled");
$('#' + id2)
.show()
.find('input, select')
.filter(":visible")
.removeAttr("disabled");
}else{
$('#' + id1)
.hide()
.find('input, select')
.attr("disabled","disabled");
}
1 Answer