6

I'm able to hide the 'tr' when 'Remove' is clicked. with the following code.

$("a#minus").bind("click", function(e){
    $(this).closest('tr').hide();
});

But I also want to clear the content of the 2 text boxes (id of the textbox's are dynamic [frm_Expense_expensesVO___strAmount and frm_Expense_expensesVO___memo] here '*' goes from 1 to infinity). Please help. Thanks

<table>
<tr>
    <td>
        Amount
    </td>
    <td>
        Memo
    </td>
    <td>    
        &nbsp;
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[0].strAmount" value="2.30" id="frm_Expense_expensesVO_0__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[0].memo" value="Five" id="frm_Expense_expensesVO_0__memo"/>
    </td>
    <td>
        <a id="minus" href="#">Remove</a>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[1].strAmount" value="3.45" id="frm_Expense_expensesVO_1__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[1].memo" value="Six" id="frm_Expense_expensesVO_1__memo"/>
    </td>
    <td>
        <a id="minus" href="#">Remove</a>
    </td>
</tr>
<tr>
    <td>
        <input type="text" name="expensesVO[2].strAmount" value="" id="frm_Expense_expensesVO_2__strAmount"/>
    </td>
    <td>
        <input type="text" name="expensesVO[2].memo" value="" id="frm_Expense_expensesVO_2__memo"/>
    </td>
    <td>
        <input type="submit" id="frm_Expense_ExpenseAdd_plus" name="action:ExpenseAdd_plus" value="+"/>
    </td>
</tr>
<tr>
    <td>    
        <label id="frm_Expense_transactionVO_amount">5.75</label>
    </td>
    <td align="right">
        <input type="submit" id="frm_Expense_Cancel" name="action:ExpenseAdd_cancel" value="Cancel"/>
    </td>
    <td align="left">
        <input type="submit" id="frm_Expense_Save" name="action:ExpenseAdd_save" value="Save"/>
    </td>
</tr>

4 Answers 4

17
$("a#minus").bind("click", function(e){
    $(this).closest('tr').hide().find('input:text').val('');
});

Note: Also see darmen's answer on why the selector a#minus will not work as desired.

Sign up to request clarification or add additional context in comments.

4 Comments

Almost identical, but you beat me by 24 secs, so I concede & will delete my answer. :)
why not just $('a#minus').click(function() { ....? Is there an advantage to using bind()?
click is just a helper method, so it's identical to the bind call, but i'd wouldn't bother too much about saving a few characters. Not much to be gained there.
Using bind, you can bind a handler to multiple events like this - $().bind('keydown keyup', function(){...}). But yeah, no difference in this case.
7

You should specify a class to anchors. Binding on a single id will raise an event for the latest one.

Example:

$("a.remove").live('click', function(){
   $(this).closest('tr').hide().find("input").val("");
});

Comments

0

Use the class attribute, and select the two textboxes using that className. Then you can set the value of that collection to the null-string.

Comments

0

Is closest defined in jQuery core or a plugin?

Anyways, you could get all text boxes within that tr and then do a regex match. This should only empty the text boxes that match the id's mentioned in your question.

$("a#minus").bind("click", function(e){
    var row = $(this).closest('tr');
    $("input:text", row).each(function(item) {
        var id = item.id;
        if(/frm_Expense_expensesVO_[0-9]+__strAmount/.test(id)) {
            $(item).val('');
        }
        else if(/frm_Expense_expensesVO_[0-9]+__memo/.test(id)) {
            $(item).val('');
        }
    });
    row.hide();
});

1 Comment

closest() is in jQuery core 1.3 onwards - docs.jquery.com/Traversing/closest

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.