html - Cannot read property '0' of undefined -- JavaScript -
my goal: upon checking checkbox, pertinent element enabled.
this javascript. understand it, this
input. calling parentnode
on going checkbox
div. set parent_1
col-md-3
div. set child
next sibling parent_1
, next col-md-3
div. error
uncaught typeerror: cannot read property '0' of undefined
the error location input.
function selectelement(object) { var parent = object.parentnode; var parent_1 = parent.parentnode; var child = parent_1.nextsibling; if (parent.children[0].checked == true) { child.children[0].disabled = false; } else { child.children[0].disabled = true; } }
this html:
<div class="col-md-3"> <div class="checkbox checkbox-primary"> <input type="checkbox" id="fast_dd" onclick="selectelement(this);"> <label for="fast_dd">anomalies</label> </div> </div> <div class="col-md-3"> <select name="fast" class="selectpicker" disabled> <option>options</option> </select> </div>
edit - not @ sure why wrote parent.children[0]
instead of object
, clumsy of me
edit 2 - changed title more accurate
you there.
what want next element, not next dom node (which text node).
use js instead:
function selectelement(object) { var parent = object.parentnode; var parent_1 = parent.parentnode; var child = parent_1.nextelementsibling; //nextelementsibling instead of nextelement if (object.checked == true) { // use object instead of parent.children child.children[0].disabled = false; } else { child.children[0].disabled = true; } }
here fiddle: http://jsfiddle.net/1hzmwd7w/2/
Comments
Post a Comment