Reputation: 199
I want to target the jQuery selector class and change it some for a purpose. My code is in php string like below.
<?php echo '<span class="demo"><script>jQuery(document).ready(function($){ $(".dt").iconpicker(); });</script></span>';?>
I know there is attr
selector but it may work on img
or input
and other tags . How about the script tag ? Is it possible ? Please suggest me ...
Upvotes: 0
Views: 221
Reputation: 9664
I'm not sure why would you do this, however the answer is YES it is possible to pick the script
tag using Element Selecting, with no id #
nor class .
prefixes, just like $('span.demo>script')
or $('span.demo script')
sure you can omit the span word and write it like $('.demo...)
.
As shown in this PHP Fiddle - hit run or F9 to execute - I was able to pick the script tag and alter its inner html (or text) - check this script element with the dev tool inspecter - but I couldn't execute the new injected function myColor()
as you can see in this image:
Code:
<?php
echo '<span class="demo"><script>alert("My original span next");</script></span>';
?>
<script>
var $html = "myColor();";
$('span.demo>script').html($html);
function myColor(){
$('span.demo').css({'background-color':'#FBB','border-color':'red'});
}
</script>
EDIT:
Alternatively you can give that script
tag an id
attribute and pick it directly, just like in this JS Fiddle again check the span.demo
with the dev tool inspector. check these two links for more information:
What is the point of using an ID attribute in a script tag?
Upvotes: 1