user1400191
user1400191

Reputation: 169

how to get an index of html element in jquery

i have html elements

<ul>
<li>
    <dl class="details clear">
        <dt>TIME&nbsp;:&nbsp;&nbsp;&nbsp;</dt>
        <dd>09:00:00-10:00:00</dd>
        <dt>Availible&nbsp;Days&nbsp;:&nbsp;&nbsp;</dt>
        <dd>monday,tuesday,wednesday</dd>
        <dt></dt>
        <dd>
        <label class="edit-details white_blue" style="float:right;margin-right:10px;">EDIT</label>
        <label class="delete-details white_red" style="float:right;margin-right:10px;">DELETE</label>
        </dd>
    </dl>
    <dl class="details clear">
        <dt>TIME&nbsp;:&nbsp;&nbsp;&nbsp;</dt>
        <dd>10:30:00-11:30:00</dd>
        <dt>Availible&nbsp;Days&nbsp;:&nbsp;&nbsp;</dt>
        <dd>monday,tuesday,wednesday</dd>
        <dt></dt>
        <dd>
        <label class="edit-details white_blue" style="float:right;margin-right:10px;">EDIT</label>
        <label class="delete-details white_red" style="float:right;margin-right:10px;">DELETE</label>
        </dd>
    </dl>
</li>
</ul>

i want to get the index of edit label, if it is in the first "dl" must alert 0 else if it is in the second "dl" must alert 1

So far i did

$("label").click(function () {
    var parent_el = $(this).parents('li');
    alert(parent_el.index(this));
});

Please help

Upvotes: 0

Views: 198

Answers (1)

user1432124
user1432124

Reputation:

$("label").click(function () {
    var parent_el = $(this).parents('dl');
    alert($("dl").index(parent_el));
});

OR

$("label").click(function () {
        var parent_el = $(this).parents('dl');
        alert(parent_el.index());
    });

Upvotes: 1

Related Questions