8

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.

11
  • How are you checking if it works? I can't help but get the feeling you're doing something very wrong here. Commented Sep 22, 2010 at 13:58
  • 2
    Why would you want to do this? Commented Sep 22, 2010 at 13:58
  • 1
    why do u wanna do this ?? Any reason Commented Sep 22, 2010 at 13:58
  • 1
    If you need to temporarily remove an element, you can use .detach(). If you need it to be permanent, use .remove(). Commented Sep 22, 2010 at 14:00
  • 1
    It's funny how silly questions get lots of silly answers :) [mine included] Commented Sep 22, 2010 at 15:14

9 Answers 9

9

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() + '-->');
}​
Sign up to request clarification or add additional context in comments.

4 Comments

your hideIt is valid and does answer this, but reversing it is not as easy as if you use .detach() (see my example for one option/use pattern)
@Mark - Like I said, I'd never do anything like this - I was just showing how to add HTML comments like the OP wanted to do.
Yep, agree, why I upvoted this answer. See my (newest) answer for a more direct/possibly better "solution" to this which removes the depenancy on the outside div.
Thanks, this is what I was wanting to do- So thank you, I did choose to go with a .css route to achieve what I was wanting. Display="none". I was working on building a Lazy flash loader and did it, now I need to make it better. Might post another question on that :)
5

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

3 Comments

+1 - exactly what I was trying to say about HTML and the DOM but better :)
Thanks, would of thanked you sooner but stackoverflow didn't email me confirmations. I ended up exploring the internet and found a .css solution. Style.display="none". The reason I needed to do this was to make a lazy like loader for flash which I accomplished- I just need to perfect it now.
@user455046: "...I ended up exploring the internet and found a .css solution. Style.display="none"..." Yup, that's what hide does.
5
$("<!--your comment text here -->").insertAfter(selector);

or

$(selector).after("<!--your comment text here -->");

live demo

Comments

1

No, you can't add a starting and ending commend tag like that. The comment tag is a single tag, it's not a seprate starting tag and ending tag. So, <!-- and --> are not a valid HTML fragments, as they don't contain complete tags.

If you want to put an element inside a comment, it's no element any more, so to do that you would have to get the HTML code for the element, put a comment tag around it, and replace the element with the comment.

If you do this to hide the element, you should simply use the hide method instead.

Comments

0

Using .hide() is a better way to achieve what you want. Also worth mentioning that comment objects are part of the DOM so in theory you could manipulate them (or add new ones) using jQuery.

Comments

0

Some options:

$('.mycurrentclass').remove(); 

$('.mycurrentclass').detach();// see below for possible use

$('.mycurrentclass').css({display:'none'});

$('.mycurrentclass').hide(); 

$('.mycurrentclass').toggleClass('myhiddenclass');

$('.mycurrentclass').addClass("myhiddenclass");

$("myselector").toggle(
  function () {
    $(this).addClass("myhiddenclass");
  },
  function () {
    $(this).removeClass("myhiddenclass");
  }
);

// programtic like you seem to want:(first part puts it back, else detaches it)
var p;
if ( p ) 
{
  p.appendTo("mywheretoappenditselector");
  p = null;
} 
else 
{
  p = $("myselector").detach();
};

Comments

0

To provide a more direct answer, use:

var gonee = $('.myclass');
$('.myclass').replaceWith('<!--' + gonee + '-->');

see me in action: http://jsfiddle.net/YNe6w/1/

3 Comments

don't you mean '<!--' + gonee.html() + '-->'? This currently adds <!--[object Object]--> to the DOM :) [in Chrome anyway]
In IE8 compatibility mode, "goner" div is gone on the fiddle page. So in theory, the div holding the "goner" text should be the context and thus removed/hidden by the comments addition.
Mark - As @Skilldrick pointed out, the comment ends up looking like <!--[object Object]--> instead of <!--<div id='goner' class='goner'>goner</div>-->. Although using .html() wouldn't really work either.
-1

I think you would need prepend and append to do it the way you are thinking. Not sure if it would actually make things disappear though.

If you want something to not be visible, your best bet would be to use hide().

Comments

-1

pls use .html() to add html content dynamically

eg:

  $(.dv).click(function(){
 $(".dy").html("abcd");
 });
 <div class=".dy" >
 </div>

try it and hope it will work for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.