If the clients browser supports it you can do like in this fiddle:
</div>
<input type="button" onclick="javascript: create_div();" value="create"/>
<div id="div-area"></div>
<script>
function create_div() {
$('#div-area').append('<div class="chatdiv">new chat</div>');
}
$(document).ready(function() {
$('#div-area').bind('DOMNodeInserted', function(e, o) {
$(e.target).css('background', 'red');
});
});
</script>
</div>
According to your comment you need to register to the onclick. This is what live()
is for:
</div>
<input type="button" onclick="javascript: create_div();" value="create"/>
<div id="div-area"></div>
<script>
function create_div() {
$('#div-area').append('<div class="chatdiv">new chat</div>');
}
$(document).ready(function() {
$('#div-area div.chatdiv').live('click', function(e, o) {
$(e.target).css('background', 'red');
});
});
</script>
</div>
jsfiddle.
, with- . Nothing has ids. The div that I want to attach a handler to has a class, not an id. I hope that helps.
– Trevor Newhook Jun 15 '12 at 14:14