1

I would like to create a button dynamically using JavaScript and jQuery.

The code below is the final piece to a quiz and when the quiz is finished, it gives the results and states you have finished. I would like to create a button dynamically using JS during this time which takes the user back to the home page (index.html).

Any help is greatly appreciated.

function displayFinalSlide(){

    $(stage).append('<div class="questionText">You have finished the quiz!<br><br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div>');

}//display final slide
2
  • 3
    Take a guess! You've come this far! That statement is adding a bit of HTML to the document. You want to add some more html; you need the markup to add a button/anchor. Commented May 19, 2015 at 4:14
  • Just add markup of button. Commented May 19, 2015 at 4:15

3 Answers 3

2

Try this: Like you are adding the html, Just add the anchor tag/Button as well

function displayFinalSlide(){

    $(stage).append('<div class="questionText">You have finished the quiz!<br><br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div><a href="index.html">Index Page</a>'); // here in href you can give the path where you want to redirect to

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

Comments

0

You could create an invisible button that get you back to the home page:

<button id="finalButton" style="visibility:hidden">Go back</button>

and then in the function "displayFinalSlide()" above:

$('#finalButton').show();

I have omitted the jquery for the button click to get back to the main site.

Comments

0

You can replace the following code in your code

function displayFinalSlide(){
$(stage).append('<div class="questionText">You have finished the quiz!<br>   <br>Total questions: '+numberOfQuestions+'<br>Correct answers: '+score+'</div>
<br/><button type="button" id="goback"> Home </button> '); } }); $("#goback").on("click",function(){window.location.assign('index.html');})

Comments

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.