Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if $(this) is nth-child(x) [duplicate]

Tags:

jquery

css

I am working on a website and I've ran into a problem.

I have a 12x24 table using nth-child() and I want to use jQuery to check which nth-child() is being clicked.

Currently I have tried:

$('table.masteries tr.p0 td').click(function() {
    if($(this) == $('table.masteries tr.p0 td:nth-child(1)')) {
        console.log('true')
    }
});

But I get no response in the console.

The table is layed out as follows:

    <table class="masteries">
        <tr class="p0">
            <td></td>
...
        </tr>
        <tr class="p4">
            <td></td>
...
        </tr>
        <tr class="p8">
            <td></td>
...
        </tr>
        <tr class="p12">
            <td></td>
...
        </tr>
        <tr class="p16">
            <td></td>
...
        </tr>
        <tr class="p20">
            <td></td>
...
        </tr>
    <table>

So I wondered if it wasn't possible to check if $(this) can work in if-statements or not. If it isn't, is there another way of checking for certain nth-child() with jQuery/Javascript if-statements?

like image 600
SaiyanToaster Avatar asked Aug 12 '14 14:08

SaiyanToaster


People also ask

What is the nth child () selector used for?

The :nth-child selector allows you to select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.

What's the difference between the nth of type () and Nth child () selector?

As a general rule, if you want to select an interval of a selector regardless of the type of element it is, use nth-child . However, if you want to select a specific type only and apply an interval selection from there, use nth-of-type .

How do you select the nth child?

The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

How do you use the nth child in sass?

:nth-child() Back You can pass in a positive number as an argument to :nth-child() , which will select the one element whose index inside its parent matches the argument of :nth-child() . For example, li:nth-child(3) will select the list item with an index value 3; that is, it will select the third list item.


2 Answers

You can use jQuery's .index() method to find what index the current element has, or use jQuery's .is() method to perform a check using CSS selectors.

$('table.masteries tr.p0 td').click(function() {
    var $this = $(this);
    // You don't need to use these together, both should work independently
    if ($this.index() === 0 || $this.is(':nth-child(1)')) {
        console.log('true')
    }
});
like image 164
SeinopSys Avatar answered Sep 27 '22 21:09

SeinopSys


I suggest using $(this).index() to get the index of the element in relation to it's siblings.

.index() Documentation

For example to check if it's the first one...

$('table.masteries tr.p0 td').click(function() {
    if($(this).index() == 0) {
        console.log('true')
    }
});
like image 22
ksloan Avatar answered Sep 27 '22 21:09

ksloan