AbuMariam AbuMariam - 4 months ago 14
jQuery Question

Javascript debugging: Printing out the element retrieved from a jQuery selector

I want to get an HTML element with a jQuery selector and print out all its contents (either in an alert or console debug). Something like this...

<div class="mydiv">Hello is it me you are looking for?</div>
<script>
alert("But I still haven't found what I am looking for " + $("#mydiv").html());
</script>


I also tried printElement as suggested in this post, but that doesn't seem to work either.

Here is a simple fiddle.

Answer

The # is the id selector. You have a class (<div class="mydiv">) so you need to use the class selector .:

alert("But I still haven't found what I am looking for " + $(".mydiv").html());
Comments