views:

519

answers:

4

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>

+2  A: 
$("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.

Chetan Sastry
Alconja
why not just `$('a#minus').click(function() { ....`? Is there an advantage to using `bind()`?
carillonator
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.
Anurag
Using bind, you can bind a handler to multiple events like this - `$().bind('keydown keyup', function(){...})`. But yeah, no difference in this case.
Chetan Sastry
Thanks you so much guys :)
Anand
cool, thanks for the bind() info.
carillonator
A: 

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.

geowa4
+2  A: 

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("");
});
Darmen
Thanks you so much guys :)
Anand
+1. I didn't notice that. I will add a note in my post.
Chetan Sastry
A: 

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();
});
Anurag
`closest()` is in jQuery core 1.3 onwards - http://docs.jquery.com/Traversing/closest
Chetan Sastry