Say I have a something like this:
<p id="script">$("p").css("color", "red");</p>
Is there a way to select the script contained within the tag and execute it using jQuery? In this case, the script
$("p").css("color", "red");
would be executed and then cause itself to be rendered with a red font color within the paragraph tag. I can select the text perfectly fine, but haven’t found a way to actually execute it. I don’t want other solutions – I am aware of them, I am just trying to figure out if this specific case is possible, and if so, how. Thanks.
Since it uses
text(), it should strip out any HTML so you should be able to apply any code highlighters to the code if you wanted to.For example, if you were using the StackOverflow code highlighter and wanted to select the code, text() would return what you expect:
Whereas
html()would probably returnWhich obviously can’t be evaluated.
If you don’t plan on doing any code highlighting then it’s a non-issue.