5

I have created a "Wizard" using JavaScript and based on people's answers you get taken to certain results divs. It works the way I want, but this code is VERY repetitive. Is there a way to clean up this JavaScript code?

$(".hidden").hide();

$(function() {

    $("#start_button").click(function(){
            $("#wizard_start").hide();
            $("#Q1").show();
    });

$("#reset").click(function(){
        $("#wizard_start").show();
        $(".hidden").hide();
        $(":input").not(":button, :submit, :reset, :hidden").each( function() {
        this.value = this.defaultValue;     
});

});

$("#q1_button").click(function(){
        if ($("input[value='q1_1']:checked").val()){
            $("#Q2").show();
            $("#Q1").hide();
        }
        else if ($("input[value='q1_2']:checked").val()) {
            $("#results1").show();
            $("#Q1").hide();
        }
        else if ($("input[value='q1_3']:checked").val()) {
            $("#Q3").show();
            $("#Q1").hide();
        }
});

$("#q2_button").click(function(){
        if ($("input[value='q2_1']:checked").val()){
            $("#results2").show();
            $("#Q2").hide();
        }
        else {
            $("#results3").show();
            $("#Q2").hide();
        }


});

    $("#q3_button").click(function(){
        if ($("input[value='q3_1']:checked").val()){
            $("#Q4").show();
            $("#Q3").hide();
        }
        else {
            $("#results1").show();
            $("#Q3").hide();
        }

});

$("#q4_button").click(function(){
        if ($("input[value='q4_1']:checked").val()){
            $("#Q5").show();
            $("#Q4").hide();
        }
        else {
            $("#Q6").show();
            $("#Q4").hide();
        }

});

$("#q5_button").click(function(){
        if ($("input[value='q5_1']:checked").val()){
            $("#results4").show();
            $("#Q5").hide();
        }
        else {
            $("#Q7").show();
            $("#Q5").hide();
        }

});

$("#q6_button").click(function(){
        if ($("input[value='q6_1']:checked").val()){
            $("#Q8").show();
            $("#Q6").hide();
        }
        else {
            $("#Q9").show();
            $("#Q6").hide();
        }

});

$("#q7_button").click(function(){
        if ($("input[value='q7_1']:checked").val()){
            $("#results4").show();
            $("#Q7").hide();
        }
        else {
            $("#results5").show();
            $("#Q7").hide();
        }

});

$("#q8_button").click(function(){
        if ($("input[value='q8_1']:checked").val()){
            $("#results6").show();
            $("#Q8").hide();
        }
        else {
            $("#results7").show();
            $("#Q8").hide();
        }

});

$("#q9_button").click(function(){
        if ($("input[value='q9_1']:checked").val()){
            $("#results8").show();
            $("#Q9").hide();
        }
        else if ($("input[value='q9_2']:checked").val()) {
            $("#Q10").show();
            $("#Q9").hide();
        }

        else if ($("input[value='q9_3']:checked").val()) {
            $("#results3").show();
            $("#Q9").hide();
        }

});

$("#q10_button").click(function(){
        if ($("input[value='q10_1']:checked").val()){
            $("#results9").show();
            $("#Q10").hide();
        }
        else {
            $("#results3").show();
            $("#Q10").hide();
        }

});

$("#q2_backbutton").click(function(){
    $("#Q1").show();
    $("#Q2").hide();
});
$("#q3_backbutton").click(function(){
    $("#Q1").show();
    $("#Q3").hide();
});
$("#q4_backbutton").click(function(){
    $("#Q3").show();
    $("#Q4").hide();
});
$("#q5_backbutton").click(function(){
    $("#Q4").show();
    $("#Q5").hide();
});
$("#q6_backbutton").click(function(){
    $("#Q4").show();
    $("#Q6").hide();
});
$("#q7_backbutton").click(function(){
    $("#Q5").show();
    $("#Q7").hide();
});
$("#q8_backbutton").click(function(){
    $("#Q6").show();
    $("#Q8").hide();
});
$("#q9_backbutton").click(function(){
    $("#Q6").show();
    $("#Q9").hide();
});
$("#q10_backbutton").click(function(){
    $("#Q9").show();
    $("#Q10").hide();
});

});

http://jsfiddle.net/dswinson/PXp7c/56/

Also, is there a way to add a "Back to Start" button on the results divs that take you back to the beginning and resets all of the radio buttons?

Thank you!

Dawn
  • 175
  • 1
  • 14
  • 5
    Live links are a great *adjunct* to a question, but always post the relevant code *in the question* as well (even if it's repetitive). Two reasons. 1. People shouldn't have to follow a link to help you. 2. StackOverflow is meant to be a resource not just for you now, but for others having a similar issue in the future. External links can get moved, modified, deleted, etc. By making sure the relevant code is in the question, we ensure that the question (and its answers) remain useful for a reasonable period of time. – T.J. Crowder Jun 22 '11 at 17:02
  • 2
    Seems that this belongs on [codereview](http://codereview.stackexchange.com/). – user113716 Jun 22 '11 at 17:04
  • I have added my code into the question, but thanks for the link. I will post my question there. – Dawn Jun 22 '11 at 17:10
  • As I answered on: http://stackoverflow.com/questions/6378814/wizard-start-over-buttons/7461074#7461074 – Kalle H. Väravas Sep 18 '11 at 14:07

1 Answers1

1
$(".hidden").hide();

This is actually quite unnecessary. I'd recommend that you add the following CSS rule instead :

.hidden{
  display : none;
}

But that's not all that's wrong with your code, I'll be brutally honest its awful. It would be better if you made some effort learning jQuery. You can't really expect people here to spend their time optimizing code that was generated by some wizard.

My answer isn't really helpful but you'd need to put in slightly more effort yourself in order to get decent help.

Edit There seem to be subtle differences in each of your functions making it harder to generalize. I'll give you a suggestion write a function like

function switch_question(current_question,next_question){
  $('#Q'+current_question).hide();
  $('#results'+current_question).show().hide(10000);
  $('#Q'+next_question).show();
};

Now instead of changing to the next question by typing the entire thing you could just pass the Question Numbers to the function and call it.

There can be more optimizations of course, you have to refactor code where ever possible. My apologies for being too hard on you, I thought you'd used some code generation tool. Hope this is more helpful.

nikhil
  • 8,925
  • 21
  • 62
  • 102
  • 1
    It wasn't generated by some wizard, I wrote the code myself. And like I said, I'm not an expert programmer with jQuery that is why I was hoping to at least get some help on where to look or what kind of code I should be looking at. I have searched the web for a long time trying to find a code that's even similar to how I want mine to work, but I can't. I have seen 1 radio selected, 1 div show. I can do that, but the way my code needs to work seems a bit complicated. I'm just saying it would be more helpful if you can tell me what I should be looking for, rather than just tell me I need help. – Dawn Jun 22 '11 at 17:45
  • In that case its not all that bad after all. I'll update my answer with some code you can look to improve. – nikhil Jun 22 '11 at 17:54
  • I know I may be asking a lot, but if anyone can just look at the way my code works now (in the demo link I posted under the code) and tell me how they would go about programming that, it would be helpful. Maybe jQuery isn't the best way to get this done efficiently and it can be done better with another type of programming language. I don't know. But it would help if I had some type of direction to go. Thank you! – Dawn Jun 22 '11 at 17:55
  • You can do it all with jQuery, there's nothing with using it. The key has to be group together similar code and then seeing wherever generalizations may be possible. And you're not asking for a lot. I shouldn't've been so assuming. My apologies once again. – nikhil Jun 22 '11 at 18:00
  • Thanks. Yeah I think my problem with writing this code is because there are so many subtle differences that I feel like I CAN'T generalize it and that I have to be specific as to which question each radio button takes you to. The next_question depends on which radio button was selected before it. So if radio 1 was selected, it can take you to question 2, radio 2 can take you to question 5, and radio 3 can take you to question 6. And it will continue like that until it gets to the results page. – Dawn Jun 22 '11 at 18:22