I have got several select tags and divs in my html:
<div>
<select name="one" id="oneId" class="selectOne" onchange="myFunction()">
<option value="" disabled selected>choose*</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<div class="show" style="display:none;">
<input type="number" value="" id="other"></input>
</div>
</div>
Now I would like to do semething like this:
function myFunction() {
var a = this.value; // to select this "select tag" of this function
var b = this.closest("show"); // to select closest "show div" to select tag
var c = b.children; //input // to select input in "show div"
if (a == 1) {
b.style.display = "block";
c.removeAttribute("disabled");
} else {
b.style.display = "none";
c.setAttribute('disabled','disabled');
}
}
function myFunction() {
var a = this.value; // to select this "select tag" of this function
var b = this.closest("show"); // to select closest "show div" to select tag
var c = b.children; //input // to select input in "show div"
if (a == 1) {
b.style.display = "block";
c.removeAttribute("disabled");
} else {
b.style.display = "none";
c.setAttribute('disabled', 'disabled');
}
}
<div>
<select name="one" id="oneId" class="selectOne" onchange="myFunction()">
<option value="" disabled selected>choose*</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<div class="show" style="display:none;">
<input type="number" value="" id="other"></input>
</div>
</div>
The main problem is with selecting proper elements (read the comment tags)