I have this .js file called by my page. The function I'm caling with my onclick is working before the button is clicked.

window.onload = function() 
{
    document.getElementById('clickMe').onclick = testOne();
}

function testOne()
{
    var para = document.getElementsByTagName('p');
    var length = para.length;

    for(i=0; i<length; i++)
    {
        document.writeln("+");
        //document.getElementById(para[i]).style.font = "Arial";
    }
}

And, my HTML:

    <html> 
        <head> 
            <title>Test.JS</title> 
            <script type="text/javascript" src="test.js"></script> 
        </head> 
        <body> 
            <div id="main"> 
                <h1 id="title">Test.JS</h1> 
                <p id="first">Hello,</p> 
                <p id="second">Again.</p> 
                <p id="third">Hello.</p> 
            </div> 
            <div id="form"> 
                <input type="text" id="txtInput" /> 
                <input type="submit" value="Click Me" id="clickMe" /> 
            </div> 
        </body> 
    </html>

Thank you,
Doug

Inadvertent

Not sure if this is right but try emoving brackets:

document.getElementById('clickMe').onclick = testOne;

DaveAmour, thank you. Couldn't see the tree for the forest.

Now... On to my next mistake.

I think what is happening here is that javascript is seeing the document.getElementById('clickMe').onclick = testOne(); and is assigning the document.getElementById('clickMe').onclick value to whatever testOne(); returns. so just try doing this:

<input type="submit" value="Click Me" id="clickMe" onclick="testOne()"/>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.