2

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!

2
  • 2
    Is there any error thrown in showSomething()? Commented Oct 1, 2012 at 18:19
  • I would prefer defining an third function that is triggered on page load and call the other needed routines. Commented Oct 1, 2012 at 18:20

3 Answers 3

5

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...

Sign up to request clarification or add additional context in comments.

2 Comments

I don't see how changing the it from one statement to 3 makes a difference. The comma operator would evaluate one expression after the other. I think there is an error in showSomething() which prevents showOnLoad() from executing.
Thanks for the reply. It works in Firefox and have no errors in my script. But, When I run it in eclipse one function doesn't work.
2

Take out the commas, and replace them with semicolons:

<body onload="document.f.firstfield.focus();showSomething();showOnLoad();">

5 Comments

no idea. you wrote the same answer as the other guy basically.
You'd probably say that 2+2 = 4, the same as me. It doesn't make you wrong or any less reputable, though ;-)
@Kristian He answered 20 seconds later than I did, so it's not that he copied or anything...we were writing them at the same time and I finished quicker somehow.
Comma separated statements are perfectly valid, although in this case I would have used semicolons as well.
i was one of the upvoters. i was saying "i dont know why you got downvoted" because I believe he shouldn't have been downvoted.
1

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);
    }
}

4 Comments

No errors were found. I changed the order it skips the showSomething() and goes through the showOnLoad()
@user1609085 So when you place showSomething() at the back it runs showOnLoad()? That basically proves that an error occurs inside showSomething().
Firebug is not showing any error? And the function is same as the other function except the checkbox id and table id
@user1609085 The code document.getElementById('chk').checked could fail if getElementById('chk') is null, so it's possible still; it's just that your browser seems okay with it, dunno

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.