Tim
Tim

Reputation: 699

Binding an event for childs of elements that has same class

I want to bind a click event for all seconds elements of a specific class and I'm using;

$("ul.message-list-row > li:eq(1) > div").click(function(){...});

bun unfortunately it doesn't work. Also I tried the same without ">" but no luck.

There are 7 identical UL in my code. So,

console.log($("ul.message-list-row").length);

returns 7, but

console.log($("ul.message-list-row > li:eq(1)").length);

returns 1. Shouldn't be 7 as well ? What is the mistake ?

I am using trying it on the following markup by the way;

<ul class="message-list-row rounded-corners">
    <li>
        <img src="Public/CSS/Images/sample-app-icon-02.png" alt="" title="" />
        <span class="app-name">AppName</span>
        <span class="version">V3.1</span>
        <div class="apple">OS V. - 5.04</div>
    </li>
    <li>
        <div>
            <span>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.</span>
        </div>
    </li>
    <li>
        <div class="progress-bar" title="70"></div>
        <span>%70</span>
    </li>
    <li class="number">7643</li>
    <li class="completed">&nbsp;</li>
    <li>&nbsp;</li>
    <li>
        <ul>
            <li>Language:</li>
            <li>
                <img src="Public/CSS/Images/flag-uk.png" alt="" title="" />
                English
            </li>

            <li>Operator:</li>
            <li>Operator</li>

            <li>Country:</li>
            <li>
                <img src="Public/CSS/Images/flag-tr.png" alt="" title="" />
                Country
            </li>

            <li>Badge:</li>
            <li>Clean</li>
        </ul>
    </li>
</ul>


<ul class="message-list-row rounded-corners">
    <li>
        <img src="Public/CSS/Images/sample-app-icon-03.png" alt="" title="" />
        <span class="app-name">AppName</span>
        <span class="version">V3.1</span>
        <div class="apple">OS V. - 5.04</div>
    </li>
    <li>
        <div>
            <span>Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor. Nulla vitae elit libero, a pharetra augue. Maecenas sed diam eget risus varius blandit sit amet non magna.</span>
        </div>
    <li>
        <div class="progress-bar" title="0"></div>
        <span>%0</span>
    </li>
    <li class="date">00:22:58</li>
    <li class="waiting">&nbsp;</li>
    <li>
        <a class="settings-button" href=""></a>
        <a class="approve-button action-button" href=""></a>
        <a class="cancel-button action-button" href=""></a>
    </li>
    <li>
        <ul>
            <li>Language:</li>
            <li>
                <img src="Public/CSS/Images/flag-uk.png" alt="" title="" />
                English
            </li>

            <li>Operator:</li>
            <li>Operator</li>

            <li>Country:</li>
            <li>
                <img src="Public/CSS/Images/flag-tr.png" alt="" title="" />
                Country
            </li>

            <li>Badge:</li>
            <li>Clean</li>
        </ul>
    </li>
</ul>

Upvotes: 0

Views: 43

Answers (1)

Matt Ball
Matt Ball

Reputation: 359776

:eq() does not work as you seem to think it does. Use :nth-child(2) instead of :eq(1).

The :nth-child selector gets every element within the jQuery set that is the nth-child of its parent. The :eq selector only ever gets one element within the jQuery set, the "nth" (0-indexed) element.

http://jsfiddle.net/mattball/xhYaP/

Upvotes: 1

Related Questions