0

I am trying to create a jsp page that generates a list of questions. The questions are received from the database correctly and the appropriate div is created. There are 55 questions.

What i want to do is make only the first questionDiv visible and set the rest hidden. Then on the submit button click the current question [1] is hidden and the question [2] is made visible.

Nothing i try in the javascript seems to have any affect. Can someone please point me in the right direction?

                <div id="questionContainer">
                    <%                        for (int ques = 1; ques < 56; ques++) {
                            out.println("<div id ='question" + ques + "' name='question" + ques + "'class='questionVisible'>");
                            out.println("<h2 class='page-header'>Question" + ques + "</h2>");
                            if (iterate.hasNext()) {

                                //out.println(row[0]);
                                //out.println(row[1]);                                                                                   
                                //out.println(row[0]);
                                String[] row = (String[]) iterate.next();
                                if (row[0].equals("1")) {
                                    out.println("<p id='question'>" + row[1] + "</p><br/>");
                                    out.println("<p id='ansA'><input type='radio'  name='answer" + ques + "' value='A' checked>" + row[2] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='B' checked>" + row[3] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='C' checked>" + row[4] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='D' checked>" + row[5] + "</p>");
                                    out.println("<button class='btn' id='btnPAQues" + ques + "' type='button' name='btnPAQues" + ques + "' onclick='onSubmitBtn();'>Submit</button>");
                                } else if (row[0].equals("2")) {
                                    out.println("<p id='question'>" + row[1] + "</p><br/>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='A' checked>" + row[2] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='B' checked>" + row[3] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='C' checked>" + row[4] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='D' checked>" + row[5] + "</p>");
                                    out.println("<p id='ansA'><input type='radio' name='answer" + ques + "' value='E' checked>" + row[6] + "</p>");
                                    out.println("<button class='btn' id='btnPAQues' type='submit' name='submit' onsubmit='onSubmitBtn'>Submit</button>");
                                } else if (row[0].equals("3")) {
                                    out.println("<p id='question'>" + row[1] + "</p><br/>");
                                    out.println("Answer: <p id='ansA'><input type='text' name='answer" + ques + "'</p><br/><br/>");
                                    out.println("<button class='btn' id='btnPAQues' type='submit' name='submit' onsubmit='onSubmitBtn'>Submit</button>");
                                }
                            }
                            out.println("</div>");
                        }
                    %>

                </div>


<script>

for(int x= 2; x <56; x++)
{
     document.getElementById("question"+x).style.visibility = "questionHidden;";
}

    document.getElementById("btnPAQues1").onclick = onSubmitBtn();
    function onSubmitBtn() {
        document.getElementById("question1").style.visibility = "hidden;";
    }
</script>

2 Answers 2

1

try this idea : firstly display all the questions add a div to each question

<div class="trigger" > <p id="question">...Submit</button> </div>

then add this JS code :

$(".trigger:first").show();

$("button").click(function() {
    var $next = $(".trigger:visible").hide().next(".trigger");
    if (!$next.length)
        $next = $(".trigger:first");

    $next.show();
});

example : http://jsfiddle.net/huH7p/9/

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

4 Comments

This works in the example, but it doesn't work when implemented on the page. Could the jsp generating the html somehow prevent javascript from interacting with the elements?
Normaly no put JS code before </body> tag in <script > tag
I tried putting it at the end before the body and above in the header. Neither work. I also can't get the jsfiddle example to work straight copy pasted into a new jsp page. Could netbeans, Tomcat, jsp have a problem with the way this works?
I found the solution. There was an additional ' that was causing a problem. Thank you for your help, it did in fact work.
0

Don't use Javascript to write out the questions. Place all the questions in standard HTML with a CSS class that makes them initially hidden, except question one. Then on each submit make the appropriate one visible while hiding the previous one.

Can you use web services? The common way to do this is use the JSP to set up the page, then use Javascript to call web services for each question. So when the page loads call for the first question. When the user clicks submit call for the next one. And so on.

6 Comments

Ideally I would prefer not to. Any idea if it is possible without web services?
This would mean that if any questions are change or upated, or the number increased or decreased there would be extensive code manipulation needed. Hence the use of a database and dynamic creation of questiondivs
Sorry when I looked at your code it looked like the Javascript was writing out the questions. Are you doing a round trip for each question?
I am getting a result set which is being sent through to the page as a vector. Then creating an iterator to use the vector accordingly. Reading each line, the (each set of 8 is a question, the first is a type indicator) and printing an appropriate div.
row is an array in [0]: contain the number of question [1]:the question [2]:choice 1 [3]:choice 2 ... ?
|

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.