views:

38

answers:

3
$('#foo').live('keyup', function (e) {
  var input = $(this).val();
  // code to process input
});

This is used in a post form and I need to run the code inside the live() when the document is ready. Is there a way, other than to wait for a key press, to invoke it?

A: 

If you're asking how to attach the keyup event after the document is ready, this is the sollution:

jQuery(document).ready(function($) {

    $('#foo').live('keyup', function (e) {
      var input = $(this).val();
      // code to process input
    });

});

However, this would have no function, because the whole idea of heaving jQuery.live is to automaticly bind to each selector, now and in the future.

Also, you're specifing an ID as the selector. The id must be unique to an element. Thus being able to bind it to multiple elements with the ID foo would be technically incorrect.

WesleyE
+1  A: 

It sounds like you're saying that there's a part of the code that will run on keyup that you also want to run once when the page loads.

If that's the case, you can place that code inside another function, and call it both on page load and inside the .live() handler.

function someFunction() {
   // code to run
}

$('#foo').live('keyup', function (e) {
  var input = $(this).val();
    // run on keyup
  someFunction();
});

   // run on page load
someFunction();
patrick dw
This is what I'm after! Thanks!
randomguy
You're welcome. :o) It looks like you haven't "Accepted" any answers to previous questions (by clicking the checkmark to the left of an answer). You may want to go back to your previous questions and do that to confirm the solution that worked for you. :o) http://stackoverflow.com/users/377920/randomguy
patrick dw
Sorry dude, but to be specific.. I asked how to trigger code inside the live and Mark's answer I believe answers this question more strictly although your solution works as well.. meh. Well, here's some kudos, lol.
randomguy
@randomguy - Not a problem, but if the element exists on page load, then why are you using `.live()`? If there's no element, then `.trigger()` won't work. If there is, then there's no reason to use `.live()`.
patrick dw
+2  A: 
$(function() { 
   // add the event
  $('#foo').live('keyup', function (e) { 
  var input = $(this).val(); 
      // code to process input 
    }); 
  $('#foo').trigger('keyup');  //trigger the event
}); 
Mark Schultheiss
Oh, wow. This is elegant. How the hell I'm supposed to decide who gets the right answer when there is two of them?
randomguy
just choose the one you like best :) one note, look into the .delegate if using latest jQuery, it might be better than .live
Mark Schultheiss
One more note: $('#foo').keyup(): is the same as the .trigger, but I find the .trigger more descriptive (easier to understand code later).
Mark Schultheiss
@randomguy - @Mark is right about choosing answers, but your use of `.live()` implies that the element is not yet part of the page. If that's the case, `.trigger()` won't work. If it is on the page, you should probably bind the `keyup` directly, and use Mark's solution.
patrick dw