JavaScript Check if last element was clicked check last element in list python,check last element in

I am trying to find out if the last td of an tr was clicked. This is my HTML example:

<table>
    <tr>
        <td>Foo</td>
        <td>Bar</td>
        <td>Clicked</td>
    </tr>
</table>

And this is my currently javascript code (not working like that):

$('table').on('click', 'tr', function(e) {
    if ($(this).find('td').is(':last-child')){
        alert("Last td clicked!");
    }
});

Note: I need to use the tr-selector for other purpose. So it it important, that the on click refers to the tr.

Answer:1

Try like following using tr td:last-child selector.

$('table').on('click', 'tr td:last-child', function (e) {
    alert("Last td clicked!");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
    <tr>
        <td>Foo</td>
        <td>Bar</td>
        <td>Clicked</td>
    </tr>
</table>
Answer:2

You don't need that .find(). Also, do you want to check if the last tdor las tr was clicked? Either way, you just have to do it like this:

$('table').on('click', 'td', function(e) {
    if($(this).is(':last-child')){
        alert("Last td clicked!");
    }
});

DEMO

Answer:3

I'm trying to config slick.js to show arrows in any circumstance. $('.slider-for').slick({ slidesToShow: 1, slidesToScroll: 1, arrows: false, fade: true, asNavFor: '.slider-nav' }); $('....

I'm trying to config slick.js to show arrows in any circumstance. $('.slider-for').slick({ slidesToShow: 1, slidesToScroll: 1, arrows: false, fade: true, asNavFor: '.slider-nav' }); $('....

  1. slick carousel always show arrows

I recently saw a listener defined inside method of object bound to nothing. Nice example is this var obj={ shout:function(){ onclick = function(){ alert("hi"); } }...

I recently saw a listener defined inside method of object bound to nothing. Nice example is this var obj={ shout:function(){ onclick = function(){ alert("hi"); } }...

please help me to build following logic, here is code: <button onclick="zoomin()">Zoom In</button> function zoomin(){ var myImg = document.getElementById("img"); var i = 0; i++; ...

please help me to build following logic, here is code: <button onclick="zoomin()">Zoom In</button> function zoomin(){ var myImg = document.getElementById("img"); var i = 0; i++; ...

I've created some sample code below. You can delete items simply by clicking the text you want to delete (or the li element). When you look at the network tab when you delete, you get back both a ...

I've created some sample code below. You can delete items simply by clicking the text you want to delete (or the li element). When you look at the network tab when you delete, you get back both a ...