javascript - How to Bind a click event to a Div inside Table Header - Stack Overflow

<table class="table lsttabl"><thead><tr><th><div id="NameLabe

<table class="table lsttabl">
    <thead>
    <tr>
        <th><div id="NameLabel">Customer Name</div></th>
        <th>Email</th>
        <th>Registered</th>
        <th id="Date1Label">Created Date</th>
        <th id="Date2Label">Last Updated Date</th>
        <th id="Date3Label">Expiry Date</th>
        <th id="UserCountLabel">User Count</th>
        <th>Active</th>
        <th>Configure</th>
        <th>Edit</th>
    </tr>
    </thead>

    <tbody> 
    </tbody>
</table> 

And I am trying to Bind Event using the code below

$("#NameLabel").click(function () { alert("hello"); });

I tried many variations but it is not working.. Please Share your knowledge.

<table class="table lsttabl">
    <thead>
    <tr>
        <th><div id="NameLabel">Customer Name</div></th>
        <th>Email</th>
        <th>Registered</th>
        <th id="Date1Label">Created Date</th>
        <th id="Date2Label">Last Updated Date</th>
        <th id="Date3Label">Expiry Date</th>
        <th id="UserCountLabel">User Count</th>
        <th>Active</th>
        <th>Configure</th>
        <th>Edit</th>
    </tr>
    </thead>

    <tbody> 
    </tbody>
</table> 

And I am trying to Bind Event using the code below

$("#NameLabel").click(function () { alert("hello"); });

I tried many variations but it is not working.. Please Share your knowledge.

Share Improve this question edited Dec 8, 2012 at 7:11 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked Dec 7, 2012 at 12:12 Aneesh MohanAneesh Mohan 1,0878 silver badges17 bronze badges 1
  • The problem is outside the code we see. There could be many reasons, from a generated html, a css preventing the click, your code not inside document ready, etc. – Denys Séguret Commented Dec 7, 2012 at 12:14
Add a ment  | 

6 Answers 6

Reset to default 4

IF this code is dynamically added after the page loads, you might need to use the delegate function.

$('body').delegate('click','#NameLabel',function(){
  alert("hello");
});

Here I am using the body selector as I can be sure that the body element was there when the page was loaded. If there is another parent element that you are loading the dynamic content into, you can use it instead of attaching the event to the body.

You need to bind it in document.ready to make sure that element is available before access by the script.

Live Demo

$(document).ready(function(){

   $("#NameLabel").click(function () {
          alert("hello");
   });

});

Try the following:

$(document).ready(function(){
    $('table.lsttabl thead th div[id$=NameLabel]').click(function(){
    alert("hii");
    });
});

Try:

$(document).ready(function(){
    $('table').on('click', 'th', function(){
        console.log(event.target);
    });
});

if you want it specifically for #NameLabel, Then change the on selector to '#NameLabel'

<script type="text/javascript">
    $(document).ready(function () {
        $('#NameLabel').click(function () {
            alert("Customer Name Clicked");
        });
    });
</script>
<table class="table lsttabl">
    <thead>
    <tr>
        <th><div id="NameLabel">Customer Name</div></th>
        <th>Email</th>
        <th>Registered</th>
        <th id="Date1Label">Created Date</th>
        <th id="Date2Label">Last Updated Date</th>
        <th id="Date3Label">Expiry Date</th>
        <th id="UserCountLabel">User Count</th>
        <th>Active</th>
        <th>Configure</th>
        <th>Edit</th>
    </tr>
    </thead>

    <tbody> 
    </tbody>
</table> 

Don't forget to import JQuery Libraries.

you could use this

here's a working jsfiddle http://jsfiddle/9PNk6/

$("#NameLabel").on('click',function () {
   alert("hello");
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742311819a4420044.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信