Niketa
Niketa

Reputation: 563

Binding mouse events to dynamically created select

I have created select tag dynamically in jquery function. I want to bind the mouse events to it. I have dynamically created the selct tag

function loadValues()
{
var sel='<select id="flavor_slct'+index+'" class="popper" data-popbox="pop1"     

onclick="alert()">'+flavor_fetched_string+'</select>';
$("#attach").append(sel);

}

I have tried using the .on() jQuery function. Still events are not triggered.

$("body").on("hover","Select",function()
    alert("hovered");
)};

How should i bind events to dynamically created elements.?

Upvotes: 0

Views: 62

Answers (2)

Akshay
Akshay

Reputation: 540

You need to use bind or on.

$("document").on("mouseenter", "#flavor_slct", function(
)};

OR

$("document").bind("mouseenter", "#flavor_slct", function(
)};

Upvotes: 0

George
George

Reputation: 36784

There is no hover JavaScript event triggered. You are probably looking for mouseenter.

You also have some incorrect syntax defining your function that I've rectified:

$("body").on("mouseenter", "select", function(){
    alert("hovered");
});

Upvotes: 1

Related Questions