Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute keyup once

Tags:

jquery

onkeyup

I'm trying to load a gif loader when I start to type in a textbox. I'm appending it to the div that contains the textbox on keyup. The problem I have is that the loader is appended every time I type a letter while I would like to append it only once when I type the first letter.

function loader(){

    $('#contrib_search').bind('keyup', function(){
        $('#contrib_text').append('<div id="loader"></div>');
    });
}

Any idea how to do this?
Thanks

like image 213
Mauro74 Avatar asked Dec 21 '22 19:12

Mauro74


2 Answers

Using 'one' could help:

function loader() {
  $('#contrib_search').one('keyup', function () {
    $('#contrib_text').append('<div id="loader"></div>');
  });
}
like image 72
Angel M. Avatar answered Jan 08 '23 05:01

Angel M.


you can unbind the keyup after you add the loader

function loader(){

    $('#contrib_search').bind('keyup', function(){
        $('#contrib_text').append('<div id="loader"></div>');
        $('#contrib_search').unbind('keyup');  //or $(this).unbind probably a tad faster
    });
}
like image 28
Rodolfo Avatar answered Jan 08 '23 04:01

Rodolfo