0

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

Edited by killingmonk

3
Contributors
4
Replies
16
Views
2 Years
Discussion Span
Last Post by Matt_17
1

Not sure if this is right but try emoving brackets:

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

0

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

Now... On to my next mistake.

0

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()"/>

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.