Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use jquery to add comments dynamically to code?

Tags:

html

jquery

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.

like image 609
user455046 Avatar asked Sep 22 '10 13:09

user455046


People also ask

Is jQuery static or dynamic?

The reason to work jquery with static content is they are known to DOM, and can handle the events of known elements. but with dynamic contents you have to bind the event with that element by using .

How dynamically add HTML element using jQuery?

$(document). ready(function() { $('body'). on('click', '. AddEl', function() { var $elems = $('.

How do you comment in jQuery?

jQuery comments are started with a double forward slash which tells the interpreter that the following text on the line is not to be processed as JavaScript. jQuery comments can be either inside a function or outside it makes no difference.

Can we use innerHTML for jQuery?

jQuery html() MethodThe html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element. When this method is used to set content, it overwrites the content of ALL matched elements.


2 Answers

It looks like you're trying to make objects with .class disappear. 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:

<h1>hello</h1>

<div>
    <p>wow</p>
    <p>dude</p>
</div>​

JavaScript (+ jQuery):

$(document).ready(function () {
    setTimeout(hideIt, 1000);
});

function hideIt() {
    $('div').html('<!--' + $('div').html() + '-->');
}​
like image 153
Skilldrick Avatar answered Nov 13 '22 01:11

Skilldrick


You're mixing two completely unrelated concepts there: HTML and the DOM.

HTML is a textual means of describing content, which — like code — has a means of commenting things out. The DOM is the resulting object model in the browser.

Once the HTML has been read and parsed and the DOM has been created, the HTML is discarded and unnecessary. Adding HTML comments to things won't change them, and in fact doesn't make sense (although it's easy to see how you thought it would, don't get the wrong idea).

Once you have the DOM, to hide an element without removing it, use hide. To remove it entirely, use remove.

Live examples of both: http://jsbin.com/araju

like image 23
T.J. Crowder Avatar answered Nov 13 '22 02:11

T.J. Crowder