2

So I have the following table, and I'd like to set all the name attributes to a new value when I add a row to the table. (You probably guessed it: I use the buttons to add and delete rows, and I calculate a new name attribute based on the row index).

<table id="orderstable" >
            <tbody>
    <tr>
        <td align="top"><img src="/images/images/add_button_sm.gif" id="add_" align="top" border="0">
                <img src="/images/images/delete_button_sm.gif" id="del_" border="0">
        </td>
        <td><input name="orderBoxes[0].A" size="3" value="0.0" type="text"></td>
        <td><input name="orderBoxes[0].B" size="3" value="0.0" type="text"></td>
        <td><input name="orderBoxes[0].C" size="3" value="0.0" type="text"></td>
        <td><select name="orderBoxes[0].D">
            <option value="fifty">50</option>
            <option value="oneHundred" selected="selected">100</option>
            <option value="twoHundred">200</option>
            <option value="threeHundred">300</option></select>
        </td>
    </tr>
            </tbody>
</table>

I've tried to use .each to get all the elements and then get at the input.name attribbute.... but I can't get it to work. Any thoughts? My code may not be the most efficient... I'm still a noob:

$("#orderstable > tbody > tr > td > img[id=add_]").click( function(event){
            var row = $(this).closest("tr").get(0);
            var rowCopy=$(row).clone(true);
            $(row).closest("tbody").append(rowCopy);

            $('#orderstable tbody>tr:last>td').each(function(){
                            $(this.input).attr(name);
                            }).val = "test";
            row.className+="add_";

3 Answers 3

11

This will grab the last row, and all input form elements. Then you can just set the name via $(this).attr('name')

$('#orderstable tr:last td :input').each(function(){
   $(this).attr('name', 'setyournamehere')
})
Sign up to request clarification or add additional context in comments.

3 Comments

This works perfectly. Thanks! SOrry I can't vote it up... not enough rep pts.
Given swatkins post above, why does the input here ('#orderstable tr:last td :input') get all input elements, including select while the other post doesn't $(e).find('input').attr('name')? Is
find('input') gets input elements by tag name. The pseudo selector :input Selects all input, textarea, select and button elements.
0

In your .each function, you need to use the arguments to get at the element and find the input:

$("#orderstable > tbody > tr > td > img[id=add_]").click( function(event){
        var row = $(this).closest("tr").get(0);
        var rowCopy=$(row).clone(true);
        $(row).closest("tbody").append(rowCopy);

        $('#orderstable tbody>tr:last>td :input').each(function(i, e){
                var n = $(e).attr('name');
            alert(n);
                        }).val = "test";
        row.className+="add_";
});

http://jsfiddle.net/swatkins/LFvwh/

2 Comments

Problem with this is that there's no input element in the first td so it's undefined, and the last td contains a select not an input. So finding an input would not work consistently.
Fair enough, it's an easy fix - add :input to the selector and remove the find method.
0

Your selector does not seem to be correct. Your table doesn't have the id 'orderstable' and does not have a <tbody>.

1 Comment

Yeh... I was trying to keep the post to a minimum. I've added it.

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.