Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

How add click event div runat server

I having difficulty in meeting add an event click on a div tag with runat = "server". This works perfectly on IE but not on chrome, mozilla .. Search error :-)

Here is the code I tried

JAVASCRIPT:

function showAlertAnomalie() {
   alert("Handler test");
}

ASP:

<div id="btn_alert_anomalie" class="alertAnomalieClass" runat="server"  onclick="showAlertAnomalie" >
   <asp:Label runat="server" Text="Alert annomaly" Font-Bold="true" Font-Size="12" ForeColor="White" style="cursor:pointer;"></asp:Label>
</div>

I ave also try this but not work.. :

$("#btn_alert_anomalie").click(function () {
   alert("Handler test");
});

Upvotes: 2

Views: 1012

Answers (3)

user1726343
user1726343

Reputation:

An inline onclick attribute is supposed to contain the body of the click handler, not the name of it. Try this:

onclick="showAlertAnomalie();"

Here is a demonstration: http://jsfiddle.net/UgcEQ/

The problem was that doing this:

onclick="showAlertAnomalie"

is equivalent to doing this:

var btn = document.getElementById('btn_alert_anomalie');
btn.onclick = function(){
    showAlertAnomalie
}

which doesn't really do anything.

Upvotes: 2

Anujith
Anujith

Reputation: 9370

Try :

$('#<%=btn_alert_anomalie.ClientID%>').click(function () {
 alert("Handler test");
});

Upvotes: 1

Usman
Usman

Reputation: 3278

try with class of div as

$(".alertAnomalieClass").click(function () {
   alert("Handler test");
});

here is FIDDLE

Upvotes: 2

Related Questions