Reputation: 13
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>
Upvotes: 1
Views: 946
Reputation: 4615
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(){})
;
Upvotes: 1
Reputation: 15714
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()
Upvotes: 0
Reputation: 14419
Here is an example of this working: http://jsfiddle.net/fPtJ8/
$(document).ready(function() {
// original code here
});
Is that what you're looking for?
Upvotes: 0