Reputation:
I am using the following code to show or hide a table depending on the checkbox is selected or not..
<body onload="document.f.firstfield.focus(),showOnLoad();">
My Javascript is:
function showOnLoad(){
if((document.getElementById('chk').checked)){
document.getElementById('div1').style.display = "block";}else{
document.getElementById('div1').style.display = "none";}
}
Its working fine till this. But, now I wanted to use another function showSomething()
which does the same functionality as above but for different checkbox and different table.
when I change my code line to this:
<body onload="document.f.firstfield.focus(),showSomething(),showOnLoad();">
both functions are not working. How can I make them both work together? Any help is appreciated!
Upvotes: 2
Views: 395
Reputation: 50905
You need to use ;
to terminate the function calls. Use:
document.f.firstfield.focus();showSomething();showOnLoad();
In normal JavaScript (not inline HTML), you may use a newline character or ;
(you can read more about that by Googling... maybe https://mislav.net/2010/05/semicolons/ will help). When you want to use inline JavaScript but need several statements, you MUST use ;
. A ,
is never used for this kind of purpose.
And not that it matters... it's just preference... but I would create a generic function that you call, say bodyLoad
, where these three statements are executed (so the HTML would become <body onload="bodyLoad();">
). It's much easier to manage when you know "everything" you want to do on load will be inside of this function. There are other things I'd do too, but I'll leave it at that...
Upvotes: 5
Reputation: 173542
If an error is thrown in the showSomething()
function, the other function is no longer executed; you can check whether any errors were thrown using the browser's JavaScript console (most browsers have them; Firebug, Chrome, F12 on IE, etc.)
One idea is to wrap the function body inside a try-catch:
function showSomething()
{
try {
// your code
} catch (e) {
alert(e);
}
}
Upvotes: 1
Reputation: 53198
Take out the commas, and replace them with semicolons:
<body onload="document.f.firstfield.focus();showSomething();showOnLoad();">
Upvotes: 2