0

I'm trying to select an id inside my table for each row.

This is my table(inspected with browser) at the moment.

<table id="sort">
            <thead>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th>Pri </th>
                </tr>
            </thead>

                        <tbody style="cursor: n-resize; " class="ui-sortable"><tr class="priorityRow">
                            <td id="tableDragSort">
                                <a class="deleteLink" href="#" rel="17904" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td>
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_0" class="17904" src="" style="height:45px;width:60px;">
                            </td>
                            <td>
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl00$txtText" type="text" value="hjjj" id="MainContent_MainColContent_ImageRepeater_txtText_0">
                            </td>
                            <td class="picturePriority">
                                1
                            </td>
                        </tr><tr class="priorityRow" style="opacity: 1; z-index: 0; ">
                            <td id="tableDragSort" style="width: 22px; ">
                                <a class="deleteLink" href="#" rel="17903" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td style="width: 60px; ">
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_1" class="17903" src="" style="height:45px;width:60px;">
                            </td>
                            <td style="width: 153px; ">
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl01$txtText" type="text" value="dd" id="MainContent_MainColContent_ImageRepeater_txtText_1">
                            </td>
                            <td class="picturePriority" style="width: 21px; ">
                                2
                            </td>
                        </tr><tr class="priorityRow" style="opacity: 1; z-index: 0; ">
                            <td id="tableDragSort" style="width: 22px; ">
                                <a class="deleteLink" href="#" rel="17895" title="">
                                 <img src="/Content/Images/Garage/DeleteButton.png"></a>
                            </td>
                            <td style="width: 60px; ">
                                <img id="MainContent_MainColContent_ImageRepeater_CarImg_2" class="17895" src="" style="height:45px;width:60px;">
                            </td>
                            <td style="width: 153px; ">
                                <input name="ctl00$ctl00$MainContent$MainColContent$ImageRepeater$ctl02$txtText" type="text" value="wggw" id="MainContent_MainColContent_ImageRepeater_txtText_2">
                            </td>
                            <td class="picturePriority" style="width: 21px; ">
                                3
                            </td>
                        </tr>
                  </table>

The id which here is 17904, 17903 and 17895 in both <a rel=""> and <img class=""> is the one I want to select into my javascript method which is here:

$("#sort .priorityRow").each(function ()
    {
        var index = $(this).index() + 1;


        var id = /* SELECT ID HERE */

        console.log(id);
        $(this).find(".picturePriority span").text(index);
        SetPicturePriority(id, index);
    });
}

I don't care which way i select the id really, as long as it works.

Anyone able to help? :)

4
  • 6
    It's not valid to use duplicate IDs on a page. Commented Nov 5, 2012 at 19:12
  • The img id will not be duplicated. Its dynamic Commented Nov 5, 2012 at 19:13
  • Does not seem to matter as it works on another site. But, the problem here is the selecting of the img id, not the ids on the table Commented Nov 5, 2012 at 19:16
  • 1
    What ID? The numbers are on the rel attribute. var id = $(this).find('a').attr('rel'); Commented Nov 5, 2012 at 19:20

2 Answers 2

1

It sounds like maybe you're trying to do this:

$("#sort .priorityRow").each(function () {
    var rel = $(this).find("a").first().attr("rel"),
        index = this.rowIndex + 1;

    $(this).find(".picturePriority").text(index);

    SetPicturePriority(rel, index);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes! var rel = $(this).find("a").first().attr("rel") is what i wanted, thanks.
1

you can get id like this

var id =$(this).attr("id");

2 Comments

i still cant figure out how i can select the rel attr on the <a> element
There's no id attribute on the tr elements, but if there was, there's no real reason to use .attr() to get the id. It's much faster and simpler to just do this.id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.