Shortest explanation possible:
You're attempting to target the child script of #target
.
For instance, if your markup were to the effect of...
<div id="target"><script type="store"></script></div>
...your jQuery selection would work.
To select ALL script elements with type of certain value do:
$('script[type="value"]')
To select a specific script that has an ID as well, do:
$('script#id[type="value"]')
One can also target the script being executed without identifiers:
<script>
// Prevent Global Scope Pollution
(function($){
// DO - Use it outside of an event...
var Script_Self = $('script').last();
console.log(Script_Self);
// DONT - Use it inside of an event...
$(document).on('ready', function() {
var Script_Self = $('script').last();
console.log(Script_Self);
});
})(jQuery);
</script>
<script>
// Prevent Global Scope Pollution
(function($){
// The event will target me :(
})(jQuery);
</script>
What you will realize when you run this code, is that the first script will target itself, and output to console, as well as target the final script in the document with the // The event will target me :(
comment.
The reason for this is that outside the event, the last element to be added to the DOM is the script tag executing the instructions, which means logically its the last script tag in $('script')
.
However, inside the event, in this case document.on('ready')
, waits until the DOM has finished loading, so that any additional script tags that may exist in the document will be loaded and would replace the desired .last()
target.
$('#target')
– Langan