I tried:
<script>
$(function() {
$('.class').before('<!--');
$('.class').after('-->');
});
</script>
but it didn’t work for a reason unknown to me.
Can anyone help me understand why it didn’t work and how I would do it? Thank you, much appreciated.
It looks like you’re trying to make objects with
.classdisappear. Use.hide()instead. Comments are only parsed when the browser first loads the page, so adding comments won’t comment something out.You need to learn the difference between HTML and the DOM. HTML is the textual representation of the page, but the browser parses it into the DOM on page load. JavaScript works on the DOM, not on the HTML. Using
.innerHtml()on DOM elements reparses the HTML.Here’s an example of using
innerHtml()to hide elements using HTML comments (but note that I would never do this – I’m only showing how to do what it looked like you were trying to do in your question):HTML:
JavaScript (+ jQuery):