My div structure should be this:
<div id="dashboard">
<img id="pic1" src="...png" />
<h6>....</h6>
</div>
To create the div I use this:
$('<div>', {
'id': 'dashboard'
}).appendTo('body');
I need to append a h6 tag with text into the above div. How can this be done? Also how can I access the text within the h6 tag when the div is clicked?
$('#dashboard div').hover(function() {
alert($(this).children().eq(2) ?? );
};
To add the
h6element, try this:To access the text of the
h6on click of thediv, try this:That assumes you are using jQuery 1.6 or lower. If you are using jQuery 1.7+, you can use
on():Example fiddle
Also, I have used
$("body")here as an example – you should use a selector which is the closest to the element you are attaching the event to (in this case#dashboard) which is not dynamically created.