1

I would like to set the script below to start on page load so that if the company name has been entered the vat field will automatically show up, could any one give me a hand on this?

//--></script> 
<script type="text/javascript"><!--
$('.colorbox').colorbox({
    width: 560,
    height: 560
});
$("input[name=company]").blur(function() {
  if($(this).val().length>0) {
    $("#vat").show();
    $("#fiscal").hide();
    $("#vat").find("input").eq(0).focus();
  } else {
    $("#vat").hide();
    $("#fiscal").show();
    $("#fiscal").find("input").eq(0).focus();
  }
});

//--></script> 
Peter O.
  • 32,158
  • 14
  • 82
  • 96

3 Answers3

1

Try this:

$(function() {
    showvat($("input[name=company]"));
    $("input[name=company]").blur(function() {
        showvat($(this));
    });
});

function showvat(element) {
    if(element.val().length>0) {
        $("#vat").show(); 
        $("#fiscal").hide();
        $("#vat").find("input").eq(0).focus();
    } else {
        $("#vat").hide();
        $("#fiscal").show();
        $("#fiscal").find("input").eq(0).focus();
    }
}

Note That:

$(function() {..}); is the same as $(document).ready(function() {});

You can also attach the function to $(window).load(function(){});

cillierscharl
  • 7,043
  • 3
  • 29
  • 47
Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
  • might be worth pointing out that `$(function() { //...` is shorthand for `$(document).ready(function(){ //...`. I prefer the more verbose version, but thats just me ;-) – Zach Lysobey Nov 25 '12 at 17:34
  • Thank YOU!!!!!!!! Nandakumar V this worked like a charm!!!!!! and thank you Zach L for the extra info – user1851475 Nov 25 '12 at 17:48
0

Here is an example of this working: http://jsfiddle.net/fPtJ8/

$(document).ready(function() {
    // original code here
});

​Is that what you're looking for?

Cymen
  • 14,079
  • 4
  • 52
  • 72
0

Since you're using JQuery, I'd probably use

$(document).ready(function(){
   // your code...
});

or

$(window).load(function(){
   // your code...
});

The former runs your code after the DOM is "ready" for manipulation. The latter runs your code after all of the page elements have loaded (images, etc...)

Check this SO post for more info: window.onload vs $(document).ready()

Community
  • 1
  • 1
Zach Lysobey
  • 14,959
  • 20
  • 95
  • 149