Lenonskie
Lenonskie

Reputation: 203

.one is not working properly on javascript

I need to execute my javascript function once and I am using this code

$('.hov').one('mouseenter', function() {
alert('You will only see this once.');
imageSliderNews.reload();
});    

but strangely it is not working properly.If I change .one to .live, then it executes correctly but I need it to execute only once. Can anyone explain why. Thanks in advance!

Upvotes: 0

Views: 231

Answers (2)

Deftwun
Deftwun

Reputation: 1232

Try calling this inside of $(document).ready()

It's possible that your elements don't yet exist in the DOM by the time you attach your handler.

Upvotes: 2

Andy Ray
Andy Ray

Reputation: 32066

You're executing the code before there's an element with class "hov" in the DOM. Move your .one() call to a place where you know the element exists.

Upvotes: 0

Related Questions