1

I'm actually doing up a questionnaire website for my own purpose. I manage to do a loop but I suppose my sequence of java script is wrong. Below is my code:

<script>
        $(document).ready(function()
        {
            var string ="";
            for(var i=2;i<12;i++){
                string +="<option value='"+i+"'>"+(i-1)+"</option>";
                console.log('test');
            }
            $("#select-question-type").change(function(evt)
            {
                var selected = $(this).val();

                if (selected == "1"){
                    $(".show-qn").show();
                    $(".show-check").hide();
                } else if(selected == '2') {
                    $(".show-check").hide();
                    $(".show-qn").hide();                    
                } else if(selected == '3'){
                    $(".show-check").show();
                    $(".show-qn").hide();
                }

            }).change();


            $("#total_qn_number").html(string);

            $("#total_qn_number").change(function(e){
                console.log("changed");
                var counter = $(this).val();
                var appendString = "";
                console.log(counter);
                for(var x=1;x<counter;x++){
                    appendString+='<br />Question '+x+':<br /><textarea name="question' +x+'" placeholder="Type your question"></textarea><br /><div class="show-qn"><label></label>A1:<input type="text" name="answer1' +x+'" placeholder="Type your 1st option" /><br /><label></label>A2:<input type="text" name="answer2' +x+'" placeholder="Type your 2nd option" /><br /><label></label>A3:<input type="text" name="answer3' +x+'" placeholder="Type your 3rd option" /><br /><label></label>A4:<input type="text" name="answer4' +x+'" placeholder="Type your 4th option" /></div><div class="show-check"><input type="text" name="cb1' +x+'" placeholder="Type your 1st option" /><br /><input type="text" name="cb2' +x+'" placeholder="Type your 2st option" /><br /><input type="text" name="cb3' +x+'" placeholder="Type your 3st option" /><br /><input type="text" name="cb4' +x+'" placeholder="Type your 4st option" /><br /><input type="text" name="noOfCorrectAns' +x+'" placeholder="Type down the no of correct ans." /></div>';
                }
                $("#container").html(appendString);
            });
        });
    </script>

It actually works pretty well but the problem lies with the show and hide. Whenever I choose my select box option, it actually "restart" the whole js itself removing all the .show and .hide functions and I've to re-click the option box for it to display normally. The picture is as below:

Before:

before clicking on choice

After:

after clicking choice

And IF i re-choose my number of question, the 'before' picture appear again which will make user irritated. Is there a way to make it stay like 'After' even if I re-choose my option?

UPDATE: Link to fiddle: jsfiddle.net/537vh

7
  • in your code, $("#select-question-type") ends with change(function(){}).change() ... do you need to write change twice?
    – Anurag
    Jul 22, 2014 at 10:16
  • Sorry, i don't understand what you meant. it have to change twice because both needs to change(if i didn't get your question wrongly)
    – Vick Peh
    Jul 22, 2014 at 10:37
  • $("#select-question-type").change(function(evt) {var selected = $(this).val(); if (selected == "1"){ $(".show-qn").show(); $(".show-check").hide(); } else if(selected == '2') { $(".show-check").hide(); $(".show-qn").hide(); } else if(selected == '3'){ $(".show-check").show(); $(".show-qn").hide(); } }).change(); this has change written twice. What happens when you remove the last .change() ?
    – Anurag
    Jul 22, 2014 at 10:41
  • Um, I don't see the change written twice on the code u copied and pasted.
    – Vick Peh
    Jul 22, 2014 at 10:55
  • 1
    If you are referring to the .change(function) and .change(), if i remove the last change, it'll display straight as first pic, instead of second pic.
    – Vick Peh
    Jul 22, 2014 at 10:57

1 Answer 1

1

IS this what you are looking for: http://jsfiddle.net/m69XP/2/

I put this in a function that gets called on change of both select boxes

$("#select-question-type").change(function(evt)
                {
                    var selected = $(this).val();

                    if (selected == "1"){
                        $(".show-qn").show();
                        $(".show-check").hide();
                    } else if(selected == '2') {
                        $(".show-check").hide();
                        $(".show-qn").hide();                    
                    } else if(selected == '3'){
                        $(".show-check").show();
                        $(".show-qn").hide();
                    }
                    else{
                        $(".show-check").hide();
                    }
                }).change();
5
  • Nope, it actually works the same as mine. You try choosing the number of question, after that you choose the question type, after that you re-choose the number of questions again. It'll go back to the first image(above) which that's not I want. I want it to show the second image no matter how many numbers of question I chose
    – Vick Peh
    Jul 22, 2014 at 11:26
  • Updating...got what you are looking for
    – Anurag
    Jul 22, 2014 at 11:27
  • Thanks in advance. I tried moving around the .change() or even removing them, it won't work. argh~
    – Vick Peh
    Jul 22, 2014 at 11:28
  • I have updated the answer. with the link to the updated fiddle. it's quite close to what you want, but will require some finishing by you. You can accept the answer if you are satisfied with the solution provided.
    – Anurag
    Jul 22, 2014 at 11:34
  • Yes that's exactly what I needed. Thank you so much for your effort!
    – Vick Peh
    Jul 22, 2014 at 11:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.