thisisnotabus
thisisnotabus

Reputation: 2069

jQuery .click on TD not firing

So I'm just trying to get a message box to pop up when I click a cell in my table. I have seen many threads about $("td").click(function () { etc, but these are not working for my table. I cannot find out why.

HTML:

<body>
<div id="tableArea">
    <table class="table table-bordered" id="myTable">
        <tr>
            <td bgcolor="green" id='a1'>1</td><td>2</td>
        </tr>
    </table>
</div>
</body>

And the javascript/jQuery:

<script type="text/javascript">
    $("td").click(function(e) {
        alert('Anything');
    });
</script>

I do not see a difference in this code than in many of the other threads, but this is not working. Note: I'm using Bootstrap, if that makes a difference.

Upvotes: 0

Views: 142

Answers (2)

Satelier Diego
Satelier Diego

Reputation: 1160

$(document).ready( function(){
    $("td").on('click', function(e) {
        alert('Anything');
    });
});

just work fine for your markup.

see it on jsfiddle

Upvotes: 0

griffla
griffla

Reputation: 589

You should capture the click event after $(document).ready().

<script type="text/javascript">
    $(document).ready(function() {
        $("td").click(function(e) {
            alert('Anything');
        });
    });
</script>

Upvotes: 3

Related Questions