I am programmer who learning jQuery javascript but never really grasped vanilla javascript (i know I am a naughty programmer). My question is how would I go about replicating this functionality in vanilla JS?
JavaScript
x
6
1
$('select').change(function() {
2
if($(this).val() == "Other (please specify)") {
3
$(this).parent().parent().find("input.hidden").show();
4
}
5
});
6
Advertisement
Answer
But here’s a step by step conversion:
JavaScript
1
13
13
1
var selects = document.getElementsByTagName('select');
2
for (var i=0; i<selects.length; i++) {
3
selects[i].onchange = function() {
4
if( this.value == "Other (please specify)") {
5
var elements = this.parentNode.parentNode.getElementsByTagName("input");
6
for (var j=0; j<elements.length; j++) {
7
if( !elements[j].className.match(/bhiddenb/)) continue;
8
elements[j].style.display = ''; // the exact thing to do here would depend on your previous actions
9
}
10
}
11
}
12
}
13