admin管理员组文章数量:1356703
I want to disable the next element of the parent of a tag:
HTML:
<span>
<input type="checkbox" onchange="disableInput(this)">
</span>
<input type="text" />
<!-- When checkbox changed, disable inputfield -->
Javascript:
<script>
function disableInput(element){
element.parentNode.nextSibling.disabled = true;
}
</script>
This didn't work. Can someone help me?
I want to disable the next element of the parent of a tag:
HTML:
<span>
<input type="checkbox" onchange="disableInput(this)">
</span>
<input type="text" />
<!-- When checkbox changed, disable inputfield -->
Javascript:
<script>
function disableInput(element){
element.parentNode.nextSibling.disabled = true;
}
</script>
This didn't work. Can someone help me?
Share Improve this question edited Feb 1, 2015 at 22:08 Deduplicator 45.8k7 gold badges72 silver badges123 bronze badges asked Apr 28, 2014 at 18:57 ThomThom 6215 gold badges14 silver badges30 bronze badges 4-
You'll find the
nextSibling
is a text node. Remove the white-space between</span>
and<input..
and you'll see results. jsfiddle/LaTsd – George Commented Apr 28, 2014 at 19:00 - 1 Remove the white space between the closing span and input and it works. – j08691 Commented Apr 28, 2014 at 19:00
- There is no error, I didn't work – Thom Commented Apr 28, 2014 at 19:00
- @j08691 It Works! But how can I fix this, because in my code is it proper to indent and break lines – Thom Commented Apr 28, 2014 at 19:03
2 Answers
Reset to default 5Try this. nextSibling
includes text nodes (including empty space), but nextElementSibling
doesn't.
function disableInput(element) {
element.parentNode.nextElementSibling.disabled = true;
}
You can use nextElementSibling
function disableInput(element){
element.parentNode.nextElementSibling.disabled = true;
}
It will select actual DOM element. The drawback - this may not be patible with older browsers like IE8.
本文标签: htmlJavaScript Get next element from ParentStack Overflow
版权声明:本文标题:html - JavaScript: Get next element from Parent - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743997255a2573219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论