I'm making an ajax call to a server page and print the result in a div via the html method:
$(document).ready(function()
{
$.ajax
({
async:false,
type: 'GET', cache: false,
url: '..urlhere..',
success: function(data){printresult(data);}
});
});
with printresult(data):
$("thediv").html(data);
This all works but the result itself contains spans with classes. These spans should throw a simple alert when they're hovered. I've implemented that too in document.ready with:
$(".spanclass").hover(function ()
{
alert('j');
});
This doesn't work.. Is it because the result comes from ajax and the DOM doesn't see it as a spanclass?
Set up a delegate
like this for dynamically added elements
$(function(){
// on document ready setup the delegate for the hover event
$(document).on('hover', '.spanclass', function(){
alert('j');
});
});
You should delegate the event, you can use on
method.
$(document).on({
mouseenter: function(){
alert('entered')
},
mouseleave: function(){
alert('left')
}
}, ".spanclass")
$('.spanclass').live('mouseover', function() {
alert('j');
});
You should use delegate
$('thediv').delegate('hover', '.spanclass', function() { ... });
This is its description from jQuery
Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.