tags:

views:

67

answers:

4

If the user clicks on "Add", I need to temporarily disable all the add/delete listeners until myid loses the focus.

<!DOCTYPE HTML>
<html>
<head>
<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;
<script type="text/javascript">
function OnLoad() {
    var myCounter = 0;
    $('.insert').live('click', function() {
        var currentRow = $(this).parents("tr:first");
        var newRow = currentRow.clone(true);
        newRow.children('td:last').empty().append('<input name="myid">');
        currentRow.after(newRow);
        document.myForm.myid.focus();
    });
    $('.delete').live('click', function() {
        if(confirm("Are you sure?")){
            $(this).parents("tr").remove();
        }
    });
}
google.load("jquery", "1");
google.setOnLoadCallback(OnLoad);
</script>
</head>
<body>
<form name="myForm">
<table border="1">
<tr>
<td class="insert">Add</td>
<td class="delete">Erase</td>
<td>Sample data goes here</td>
</tr>
</table>
</form>
</body>
</html>

In other words, I don't want them to click "Add" and then "Add" again.

+2  A: 

You might find it easier to set a variable when an input gets focus, and clear it when it looses it. The onClick functions for add and erase should check whether or not this chosen variable is set, and either return or continue depending on it.

Alternately, you can use buttons for add and remove, then set/clear the disabled attribute of the add/remove buttons when inputs receiving and loose focus.

Matt
+1  A: 
var adding = false;
$('.insert').live('click', function() {
    if(!adding){
        // Do stuff
        adding = true;
    }
}
Harmen
that won't live outside the scope of the current executing input
Jeremy B.
+4  A: 

You need to make a variable that tracks whether the user is allowed to click Add.

When Add is clicked, set the variable to false.

When the textbox loses focus, set it to true again, like this:

$('input[name=myid').live('focusout', function() { canAdd = true; }));

Then, add if(!canAdd) return; to the beginning of the Add handler

SLaks
live('focusout'), function should belive('focusout', function
cf_PhillipSenn
+1  A: 

Wrap the code of each event handler into its own function:

function insertHandler() {
    var currentRow = $(this).parents("tr:first");
    var newRow = currentRow.clone(true);
    newRow.children('td:last').empty().append('<input name="myid">');
    currentRow.after(newRow);
    document.myForm.myid.focus();
}

At the beginning of your two method handlers, unbind the click event:

$('.insert').live('click', function() {
    $(this).unbind("click");
    insertHandler();
});

Add a new handler, binding to the focusout event using live (as your input is programatically inserted), which rebinds your function to your event:

$("input[name=myid]").live("focusout", function() {
    $(".insert").live("click", insertHandler);
});
karim79
I think I need to pass 'this' to insertHandler.
cf_PhillipSenn
I got it to work (I am using Firefox) by passing currentRow to insertHandler.Thanks for your help!Stay tuned for my next question!
cf_PhillipSenn
Oh wait! For some reason, the "Add" is not unbinding.
cf_PhillipSenn
Try using `$(this).die();` instead of `$(this).unbind("click");`
karim79
$(".insert").die(); worked.
cf_PhillipSenn