0

Hi i have the following jquery code:

jQuery.noConflict()
jQuery(document).ready(function($) {
  $("#input").on("blur", function() {
     alert("item is blur");
  }
});

Now later on i have

window.onload=function() {
    jQuery("#input").blur();
}

The window.onload doesn't seem to get fire?

What is wrong? I want to be able to have events register in ready than in onload fire them depending on the loaded value? Now if i blur it manually it works, but not while the onload method is being activated.

Greg Pettit
  • 10,305
  • 4
  • 47
  • 68
Chun ping Wang
  • 3,619
  • 12
  • 39
  • 53
  • What does the JavaScript console tell you? If you're not using one, you really should be. It would identify errors you encounter and help identify typoes / syntax errors such as those in the above sample. – Greg Pettit Dec 13 '11 at 15:08

2 Answers2

3

You could do:

jQuery(window).load(function()
{
    jQuery("#input").blur(); 
});
Mark Schultheiss
  • 28,892
  • 9
  • 63
  • 88
  • Hi thanks. but i am adding jquery to existing legacy application so the onload function is define by `window.onload=function(){ alert ('loading item'); jQuery("#input").blur(); }` Now during page loading it goes to "item is blur", loading item.. than stop. I expect it to be "item is blur" after page completely finishes loading. – Chun ping Wang Dec 13 '11 at 17:43
  • btw if i do what you suggested than the blur goes into effect but i am mixing in legacy app. – Chun ping Wang Dec 14 '11 at 04:29
2

You have a syntax error:

jQuery(document).ready(function($) {
  $("#input").on("blur", function() {
     alert("item is blur");
  }); //<-- );
});

Works: http://jsfiddle.net/fkling/mvtxW/

To detect such errors, just open the console. You would have seen:

Uncaught SyntaxError: Unexpected token }

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
  • Also missing a semicolon after the noConflict() – Greg Pettit Dec 13 '11 at 15:07
  • I'd love to know how not. ;-) Or in other words, it doesn't sit right in my brain and I need to be educated-- not that I think you're wrong. – Greg Pettit Dec 13 '11 at 15:10
  • 1
    Semicolons are optional in JavaScript. [They get inserted automatically](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion). It is better to explicitly include them, but leaving them is technically not an error. – Felix Kling Dec 13 '11 at 15:13
  • Thanks, Felix. At some point in time I may have even known this, but when you get into habits (in this case, good ones I suppose) you can sometimes start to think of your habits as rules. – Greg Pettit Dec 13 '11 at 15:16
  • 2
    I know :) It gives me the shivers when I see JavaScript code without semicolons ;) – Felix Kling Dec 13 '11 at 15:18
  • One note: You can't minify JavaScript code without semicolons - or it is somewhat difficult – Mark Schultheiss Dec 13 '11 at 15:35