<div id="rnd()"><button onclick="checkId()">Click</button></div>
I have a DIV when I make by DOM in JS and has a random ID, his child this button.
I need at the click of a button I can know what the ID of the parent (DIV).
You need to update your function call to include this
:
<div id="rnd()"><button onClick="checkId(this)">Click</button></div>
<script>
function checkId(elem)
{
alert(elem.parentNode.id);
}
</script>
or add the event using javascript and give your button an id like this:
<div id="rnd()"><button id="myBtn">Click</button></div>
<script>
document.getElementById("myBtn").onclick = function(e){
alert(e.target.parentNode.id);
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With