Is there a way to get an object or it’s id within a script lies using jQuery or something?
For example:
<div id="div1">
<script type="text/javascript">
var obj = ?? // this should be the "div1" div
</script>
</div>
In this example, obj should be equal to “div1” (or the object itself).
Another one:
<p id="paragraph_7">
<script type="text/javascript">
var obj = ?? // this should be the "paragraph_7" p
</script>
</p>
In this example, obj should be equal to “paragraph_7” (or the object itself).
If I give an “id” to the script tag and then get it’s parent should work, but is there another way? not always i’m able to know the <script> id.
Thanks in advance.
Since scripts are executed sequentially, the currently executed script tag is always the last script tag on the page until then. So, to get the script tag, you can do:
You could then use this:
Part of this is from https://stackoverflow.com/a/3326554/1188942