Note that there are some explanatory texts on larger screens.

plurals
  1. POJquery Click Event on Form Input Not Running
    primarykey
    data
    text
    <p>I am creating a math problem website and in one page I have a part where the user clicks submit and it will check whether the answer is right or wrong.</p> <p>Here is the main part of my code that I am having trouble with:</p> <pre><code>var newProblem = function(){ var textHide = true; var submitTimes = 0 $("#content").append("&lt;h2 class='middle_text'&gt;Notice, this page is in BETA stage, improvement is still needed.&lt;/h2&gt;") $(".middle_text").fadeTo(1000,.8) setTimeout(function(){$(".middle_text").fadeTo(500,0,function(){$(".middle_text").hide()})},4000); setTimeout(function(){ var denominator = getNumbersEquivFrac.den() var numerator = getNumbersEquivFrac.num(denominator); var equivalent = getNumbersEquivFrac.equiv() var problem = new equivalFrac(numerator,denominator,equivalent); $("#content").append("&lt;div class ='problem-choice' id='solve'&gt;SOLVE THIS!&lt;/div&gt;") $("#content").append("&lt;div class='problem-choice' id='answer'&gt;SKIP AND GET ANSWER&lt;/div&gt;") $("#content").append("&lt;div style='margin:auto; width:450px; text-align:center'&gt;&lt;p id='question'&gt;" + problem.printQuestion() + "&lt;/p&gt;") $("#content").append("&lt;div id='instructions'&gt;&lt;/div&gt;") $("#question").fadeTo(750,1); $(".problem-choice").fadeTo(750,1); $("#solve").click(function(){ if(textHide === true){ $("#content").append("&lt;form name='answer'&gt;&lt;input class='problem-text' type='text' name='answer-input'&gt;&lt;input type='button' class='problem-submit' value ='SUBMIT ANSWER'&gt;&lt;/form&gt;"); $(".problem-text").fadeTo(300,.8) $(".problem-submit").fadeTo(300,.8) textHide = false } }) },4500) } newProblem(); $(".problem-submit").click(function(){ var checkAnswer = function(){ var answer = document.forms["answer"]["answer-input"].value; if(answer === null || answer === ""){ alert("You must type in an answer.") } else{ submitTimes = submitTimes + 1; if(answer !== problem.answer()){ if(submitTimes &lt; 2){ $("#content").append("&lt;div class='result' id='wrong'&gt;&lt;div id='problem-des'&gt;&lt;p&gt;The correct answer was " + problem.answer() + "." + "&lt;/p&gt;&lt;/div&gt;&lt;button id='next'&gt;NEXT&lt;/button&gt;&lt;/div&gt;"); $(".result").fadeTo(500,1) } } else if(answer===problem.answer()){ if(submitTimes &lt; 2){ alert("Correct!") } } } } checkAnswer() }); </code></pre> <p>This is where the event is for the form submit button</p> <pre><code>$(".problem-submit").click(function(){ var checkAnswer = function(){ var answer = document.forms["answer"]["answer-input"].value; if(answer === null || answer === ""){ alert("You must type in an answer.") } else{ submitTimes = submitTimes + 1; if(answer !== problem.answer()){ if(submitTimes &lt; 2){ $("#content").append("&lt;div class='result' id='wrong'&gt;&lt;div id='problem-des'&gt;&lt;p&gt;The correct answer was " + problem.answer() + "." + "&lt;/p&gt;&lt;/div&gt;&lt;button id='next'&gt;NEXT&lt;/button&gt;&lt;/div&gt;"); $(".result").fadeTo(500,1) } } else if(answer===problem.answer()){ if(submitTimes &lt; 2){ alert("Correct!") } } } } checkAnswer() }); </code></pre> <p>I don't know if it is the fact that the event is being called on a selector that was appended through the code and not originally in the html document. </p> <p>I tried calling the event on parts of the page that were originally there, it worked.</p> <p>But it isn't working for these, if you have any idea why, please say so.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload