Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id of javascript's container?

Tags:

javascript

I would like to get ID of javascript's container, for ex:

<div id="d_17j_a">
   <script type="text/javascript">
      alert("<ID of javascript's container here>");
      // it will alert: d_17j_a
   </script>
</div>

(ID of div is dynamic)

Thank for any suggestion !

like image 892
Rong Nguyen Avatar asked Jan 13 '23 11:01

Rong Nguyen


2 Answers

So scripts are loaded sequentially, so you can get the parent node of a script element through:

var scripts = document.getElementsByTagName('script');
var me = scripts[scripts.length-1];
console.log('parent id', me.parentNode.id);
like image 166
Jan Jongboom Avatar answered Jan 22 '23 18:01

Jan Jongboom


<script id="FindMe" type="text/javasctipt"> should work just fine using jQuery("#FindMe").parent().id

In pure javascript

document.getElementById("FindMe").parentNode.id

like image 38
Nomad101 Avatar answered Jan 22 '23 17:01

Nomad101