2

**EDIT --- Trying an even more simplified version.... Still doesn't work... Returns $(this).closest is not a function error... **

$("#fitable input[name^=f1]").focus(function() {
        var newRow = '<tr><td></td><td></td><td></td><td></td></tr>';
        $(this).closest("tbody").append(newRow);
    });

Original Post

Pretty new to jQuery, so I'm hoping someone can help me out... There's a couple of things going on here... Help with any part of it is appreciated.

For starters, I'm trying to append a row to a table when a user clicks in the first enabled input field for that row. Here's the code I'm trying to use:

    $("#fitable > tbody > tr > td > input").bind('focus', function() {
        if($(this).attr('disabled', false)) {
            $(this).click(function() {
                    var newRow = '<tr><td><input name="f1[]" value="" /><label>CustNew</label></td><td><input name="field_n1[]" value="" /><label>N1</label></td><td><input name="field_n2[]" value="" /><label>N2</label></td></tr>';
                    $(this).closest("tbody").append(newRow);
            });
        }
    });

If it's helpful, here's the html:

<table id="fitable">
    <tbody>
        <tr valign="top">
            <td><input disabled="disabled" name="cust" id="edit-cust" value="[email protected]" type="text"><label>Cust</label></td>
            <td><input name="field_n1[]" value="" type="text"><label>N1</label></td>
            <td><input name="field_n2[]" value="" type="text"><label>N2</label></td>
        </tr>
    </tbody>
</table>
4
  • try to replace closets with parents Commented Apr 24, 2010 at 18:27
  • 1
    or $(this).parents("tbody:first").append(newRow); Commented Apr 24, 2010 at 18:32
  • What @[Jim Schubert] said too! Commented Apr 24, 2010 at 18:38
  • Growl... Neither worked. No errors, and no appending. Do I need to go through the whole parent().parent()... rigamarole? Commented Apr 24, 2010 at 18:40

3 Answers 3

1

I think your if statement is setting the clicked attribute to false. Try this:

if( $( this ).attr( 'disabled' ) == false ) {
    // do stuff
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I tried that, unfortunately, same behavior... as in no appending happening... Do you see anything else it could be?
in the if statement, add console.log( 'if' ); or something.. just to test you're actually getting into that statement. Then open your console and check for the log. Or, if you don't want to use the console, do alert( 'if' ); in place of console.log... let us know what happens
alert('if') pops up, so I know I'm in the right place... I altered my code to make it even more simple, and it's still not appending... Going to edit my original post in just a sec so you can see the latest revision...
Try wrapping your append var: $( [your stuff] )
0

I think your selector is incorrect. Try this:

$("#fitable > tr td input:enabled:first").focus(function() {
    var newRow = '<tr><td><input name="f1[]" value="" /><label>CustNew</label></td><td><input name="field_n1[]" value="" /><label>N1</label></td><td><input name="field_n2[]" value="" /><label>N2</label></td></tr>';
    $(this).parents("tbody:first").append(newRow);
});

You don't have to check disabled in the handler function, because this will only bind to the enabled elements in the first place.

Comments

0

Try this:

$("#fitable input").focus(function() {
        if($(this).attr('disabled', false)) {
            $(this).click(function() {
                    var newRow = '<tr><td><input name="f1[]" value="" /><label>CustNew</label></td><td><input name="field_n1[]" value="" /><label>N1</label></td><td><input name="field_n2[]" value="" /><label>N2</label></td></tr>';
                    $(this).closest("tbody").append(newRow);
            });
        }
    });

1 Comment

Trying this returns an error: $(this).closest is not a function... (?)

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.