admin管理员组文章数量:1322354
i am trying to alert the id of a div when clicking on an image inside it as code is:
<html>
<head>
<style type="text/css">
.test1{
width:500px;
background: #cc0000;
color:#fff;
height:200px;
margin:0px auto;
}
</style>
<script>
function hidediv(){
var a = document.getElementById("divtobehide");
alert(a);
}
</script>
</head>
<body>
<div id="divtobehide" class="test1">
This is the div to be hiddenite
<img src="index.jpg" id="imgid" onclick="hidediv()" style="float:right;">
</div>
</body>
here the alert box appears but the id is not displayed. how to solve this?
i am trying to alert the id of a div when clicking on an image inside it as code is:
<html>
<head>
<style type="text/css">
.test1{
width:500px;
background: #cc0000;
color:#fff;
height:200px;
margin:0px auto;
}
</style>
<script>
function hidediv(){
var a = document.getElementById("divtobehide");
alert(a);
}
</script>
</head>
<body>
<div id="divtobehide" class="test1">
This is the div to be hiddenite
<img src="index.jpg" id="imgid" onclick="hidediv()" style="float:right;">
</div>
</body>
here the alert box appears but the id is not displayed. how to solve this?
Share Improve this question asked Feb 26, 2014 at 13:13 Sahil QureshiSahil Qureshi 251 gold badge1 silver badge3 bronze badges4 Answers
Reset to default 6When you use document.getElementById()
you already know the id
.
You could even do this:
function hidediv()
{
alert("divtobehide");
}
It doesn't make much sense the way you have written the code. Maybe there's a conceptual error (misunderstanding) on how getElementById()
works?
If you want to manipulate a DOM
object of which you already have the ID you can do this:
function hidediv(nodeId)
{
var node;
node = document.getElementById(nodeId);
node.style.display = "none";
}
In the HTML specify the related id:
<img src="index.jpg" id="imgid" onclick="hidediv('divtohide')" style="float:right;">
This way you can reuse the function and criss-cross <img>
and <div>
nodes.
try this
alert(document.getElementById("imgid").parentNode())
OP is asking to get clicked element's div ID, not how to hide an element.
Once you get an element, you can use dot notation
to access it's properties.
Here is how you do it:
var element = document.getElementById('id');
console.log(element.id);
Now, if you want an element's parent. It's a bit different. Pass the element itself as parameter of your function by adding "this" in between the parantheses.
<img src="index.jpg" id="imgid" onclick="hidediv(this)" />
Change the function header to allow the passing of the parameter. You can name it however you want althoug I suggest "sender".
function hidediv(sender){}
Now inside the function, you can get the sender's parent by using the dot notation .parentNode
. This will make your function work, whatever image is clicked or whatever the div's id. Be wary tho this only gets the immediate parent of the sender.
Take a look at this: http://jsfiddle/TASmS/
Here is simple solution.
function reply_click(clicked_id)
{
alert(clicked_id);
}
onClick="reply_click(this.id)"
本文标签: how to get html element id using javascriptStack Overflow
版权声明:本文标题:how to get html element id using javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742114027a2421385.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论