Skip to content Skip to sidebar Skip to footer

How To Make Keyup() Function Only On First Keyup In The Field

I have created a keyup() function the code is here $(document).ready(function() { var scntDiv = $('#add_words'); var wordscount = 1; $(document).keyup(function(e) {

Solution 1:

You can use the one method to unbind an event automatically after first time use:

$("#foo").one("keyup", function() {
  alert("This will be displayed only once.");
});

Solution 2:

you can make use of jQuery .one()

$('#myinput').one('keyup',function(){
 // do something
});

Solution 3:

I think this is what you serach for

$(document).ready(function() {
    var scntDiv = $('#add_words');
    var wordscount = 1;
$("#add_words").on("keyup","input[type='text']",function(e) { // Set the eventhandler to the inputsvar key = (e.keyCode ? e.keyCode : e.which);
        if (key === 32) {
            if($(this).attr("data-isused")!="true"){ // Check if THIS textbox have append a new textbox?
            $(this).attr("data-isused","true"); // Mark that this textbox has append a new one
            wordscount++;
            $('.input1').append('<p>stop touching your keyboard</p>');
            $('<div class="line">Word ' + wordscount + '<input type="text"     class="input' + wordscount + '" value="' + wordscount + '" /><a class="remScnt">Remove</a>    </div>').appendTo(scntDiv);
        //i++ Ignore this, couse its not declaredreturnfalse;
        }
    }
});

});

JS Fiddle: http://jsfiddle.net/pURVS/

Solution 4:

I'm not sure what do you want exactly, but try this...

Use HTML5 data.* to know who used that space before.

$(document).ready(function() {
    var scntDiv = $('#add_words');
    var wordscount = 1;
    $(document).keyup(function(e) {

        var key = (e.keyCode ? e.keyCode : e.which);
        if (key === 32 && $(e.target).data('added')) {  // <=== Here
            $(e.target).data('added', 'added');        // <===  And Here
            wordscount++;
            $('.input1').append('<p>stop touching your keyboard</p>');
            $('<div class="line">Word ' + wordscount + '<input type="text" class="input' + wordscount + '" value="' + wordscount + '" /><a class="remScnt">Remove</a></div>').appendTo(scntDiv);
            i++
            returnfalse;
        }
    });
});

Solution 5:

You should use bind and unbind methods:

http://api.jquery.com/bind/ http://api.jquery.com/unbind/

For example:

var handler = function() {   
  alert('The quick brown fox jumps over the lazy dog.'); 
  // unbind handler so the function is not executed again
  $('#foo').unbind('click', handler);
}; 
$('#foo').bind('click', handler); 

In your handler function, if you no longer want it to be executed (after the first key press), you call the unbind method.

Post a Comment for "How To Make Keyup() Function Only On First Keyup In The Field"