From inside a javascript file, how can we detect the parent of this script tag without any id and class by jquery.
(function() {
$(this).parent('div').attr('id');
})();
From inside a javascript file, how can we detect the parent of this script tag without any id and class by jquery.
(function() {
$(this).parent('div').attr('id');
})();
You can do it like this:
<div class="parent">
<script>
var scriptTags = document.getElementsByTagName('script');
var parentTag = scriptTags[scriptTags.length-1].parentNode;
alert(parentTag.className);
</script>
</div>
Note that you have to get the script tags at once and not on DOM ready.
In one line jQuery: var script = jQuery('script').last().parent();