zaydek zaydek - 4 months ago 10
Javascript Question

Use jQuery to set an element to itself with .html() not .append()

I'm wondering how I can leverage jQuery or JavaScript to set an element to itself using the

.html()
or a similar method – not
.append()
.

Here's an example of pseudo code (doesn't work).

<script>
var body = $("body")
$("body").html(body + "etc.")
</script>


or

<script>
$("body").html($("body") + "etc.")
</script>


What I'm attempting to do is hide the responsible
<script>
tag from the client-side DOM. If I use
.html()
, the HTML is rewritten and thus the same
<script>
tag is hidden. If I use
.append()
, that
<script>
tag is is still observable in the DOM.

This would be a huge help. Thanks!

Answer

If what you are attempting to do is to remove the unwanted script tags, then you can just write a script to remove those tags.

<script class='removeTag'>
    $(".removeTag").remove();
</script>

You can place this at end of your page and the class 'removeTag' can be added to the script tags you want to remove from DOM.