Djm Djm - 1 year ago 72
Javascript Question

Button doesn't call alert()

When I click the message button, it won't call alert() ..can anybody check the code please?

<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="lib/jquery/jquery-1.4.2.js"></script>
<script type="text/javascript" src="lib/jquery/jquery-ui-1.8.10.custom.min.js"></script>
<script type="text/javascript" src="lib/jquery/jquery.dmDropDownMenu.js"></script>
<script type="text/javascript" src="lib/jquery/watermark.js"></script>

<link rel="stylesheet" type="text/css" href="/jquery-ui-1.8.10.custom.css" />
<link rel="stylesheet" type="text/css" href="/stylez.css" />
<script type="text/javascript" /> $(document).ready( function(){ $('#buttons button').click(function(){ switch( $('buttons').id){ case "mail": alert(1); break; } }); });

<table width="1000" border="0" cellpadding="0" cellspacing="0">
<td id="columnA">

<div id="buttons">
<button id="mail" class="menubutton">Message</button><br/>



Answer Source

If you want to check which button has been clicked and you are going to have multiple buttons, you can use $(this) inside the .click(), it will reference THE object which was clicked. So change the switch to:

switch($(this).attr('id')) {

The full lot:

$('#buttons button').click(function(){
    switch( $(this).attr('id') ){
        case "mail":

See a working demo here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download