1

Second function, which should take me to player2.html, isn't working for some reason. Is there any mistake in syntax or format?

document.getElementById("start").onclick = function()
{
    location.href="player.html";
}

//**>>>**This doesn't work. Button does nothing when clicked****
document.getElementById("next").onclick = function()
{
   location.href="player2.html";

}

document.getElementById("startgame").onclick = function()
{
    location.href = "gameboard.html";
}

This is index.html

<div class="container">

    <header>
        <h1>
            Tic Tac Toe
        </h1>
    </header>


    <div class="frame">
        <div>
            <button id="start">Start</button>
        </div>
        <div>
            <button>Exit</button>
        </div>
    </div>

</div>



<script src="main.js"></script>

This is player.html

<div class="container">
        <header>
            <h1>Tic Tac Toe</h1>
        </header>
        <div class="frame">
            <label for="player">Enter player1 name : </label>
            <input type="textbox" id="player">
            <div>            
                <button id="next">Next</button>
            </div>
        </div>
    </div>

    <script src="main.js"></script>
6
  • 8
    We also need to see your HTML that contains the next button. Commented May 22, 2018 at 19:16
  • 1
    From what i see it should work. you probably need to Check the console and see if there's any errors in there, and posting your html too would help :) Commented May 22, 2018 at 19:20
  • No. It works fine,only if i put the function in different .js file but when put together doesnt work. Is there any logic behind that. Commented May 22, 2018 at 19:23
  • @saujan Yeah you probably have an error in your JS somewhere. If there are syntax errors, nothing in your script will run. If there are runtime errors, your script will stop there. Look at your debug console to see if there are any errors. Commented May 22, 2018 at 19:25
  • 1
    You player.html page does not have an element with id start, so your script will fail on the first line where it tries to set onclick on an undefined object. Commented May 22, 2018 at 19:27

1 Answer 1

1

The following code is causing an error when you load the player.html page because there's no element with an id of "start" on that page.

document.getElementById("start").onclick = function()
{
    location.href="player.html";
}

You'll get an error at the top of the JS file, which breaks the other buttons. I recommend jQuery, as it won't error when an ID isn't found when binding an onclick event. In jQuery do this.

$('#next').click(function(){
    location.href="player.html";
});

If you don't want to use jQuery, here's the JavaScript way

var elem = document.getElementById("start");

if(elem){
    elem.onclick = function()
    {
        location.href="player.html";
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

i followed your javascript way. Can u plz tell me how if statement worked??
Yes. IF returns a boolean, yes or no. If something is NULL, the IF fails. In this case, if the element is null, the IF statement fails.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.