admin管理员组文章数量:1379620
I am looking for way to change 'GetElementById' div inside the div, which I am targeting.
Here's the example:
Let's say I have this
<div onclick="runscript()">
<div id="insider">
</div>
</div>
<div onclick="runscript()">
<div id="insider">
</div>
</div>
<script>
function runscript(){
document.getElementById('insider').style.color='red';
}
</script>
If I would like to change exactly the same div, which I am clicking on, I could use this.style.color='red'
, but I need to change "insider" div inside exactly 'this' div I'm clicking on.
I am looking only for javascript solution, no jquery.
I am looking for way to change 'GetElementById' div inside the div, which I am targeting.
Here's the example:
Let's say I have this
<div onclick="runscript()">
<div id="insider">
</div>
</div>
<div onclick="runscript()">
<div id="insider">
</div>
</div>
<script>
function runscript(){
document.getElementById('insider').style.color='red';
}
</script>
If I would like to change exactly the same div, which I am clicking on, I could use this.style.color='red'
, but I need to change "insider" div inside exactly 'this' div I'm clicking on.
I am looking only for javascript solution, no jquery.
Share Improve this question asked Jan 11, 2015 at 22:15 SimonSimon 1,3744 gold badges14 silver badges28 bronze badges 2- 1 try to use querySelector – MightyPork Commented Jan 11, 2015 at 22:17
-
3
You cannot use the same id twice. It's supposed to be unique.
document.getElementById
only returns the first id it finds. – Mouser Commented Jan 11, 2015 at 22:17
3 Answers
Reset to default 2<div onclick="runscript(this)">
<div class="insider">
Sample text
</div>
</div>
Give the insider div a class
name called insider
and do this:
function runscript(object){
object.querySelector("div[class='insider']").style.color='red';
}
This works by passing the parent div to the runscript
function with the keyword this
. Then querySelector
will try to find the element based upon the selector div[class='insider']
. If found it will set the color of the text to red.
<div onclick="runscript(this)">
<div class="insider">
</div>
</div>
<div onclick="runscript(this)">
<div class="insider">
</div>
</div>
<script>
function runscript(object){
object.querySelector('.insider').style.color='red';
}
</script>
like in the ments above
- id is an unique identifier - so it is not valid to have an id twice in your DOM
- I remend you to use addEventListener instead of the onclick attribute
I made a fiddle in pure javascript: http://jsfiddle/53bnhscm/8/
HTML:
<div onclick="runscript(this);">
<div class="insider"></div>
</div>
<div onclick="runscript(this);">
<div class="insider"></div>
</div>
CSS:
div {
border: 1px solid black;
height: 100px;
}
.insider {
border: 3px solid green;
height: 20px;
}
Javascript:
function runscript(x) {
x.firstElementChild.style.border = '1px solid red';
}
本文标签: javascriptSomething like thisgetelementbyid(id of element inside this div)Stack Overflow
版权声明:本文标题:javascript - Something like this.getelementbyid(id of element inside this div) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064512a2584738.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论