1

I asked a similar question previously, but it was so vague that I couldn't possibly have gotten the answer I wanted.

Basically, I want a user to be able to select from multiple options and have various bits of information appear based on the selections.

For example:

"How old is your computer?" 
Options: [x]One Year [ ] Two Years [ ] Three Years

Output: "Your computer is One year old"

I want that functionality to expand over multiple separate checkboxes and radioboxes...?

Is this possible?

EDIT:

Its for a hardware upgrade recommendation system that takes in to account your current system.

"Based on your current system, you need to replace your graphics card and purchase additional RAM"

Which could use factors like "How old is your computer?", "How much RAM does your computer have", "What directX version is your graphics card" or something like that.

SevenT2
  • 155
  • 8
  • I formatted as code purely for readability! – SevenT2 Nov 17 '09 at 17:52
  • There are literally dozens of ways to accomplish this. It's difficult to know where to start. Check out jquery and its selectors though, I think it'll get you a long way. – Matt Brunmeier Nov 17 '09 at 17:56
  • You probably need a bit more explanation. If the user chooses option 1 for question 1 and option 2 for question 2 would the output be "Your computer is One year old AND you choose option two" OR is it going to be something altogether different? – Vincent Ramdhanie Nov 17 '09 at 18:00
  • Pretty much what you said Vincent. Its for a hardware recommendation "Based on your current system, you need to replace your graphics card and purchase additional RAM" Which could use factors like "How old is your computer?", "How much RAM does your computer have", "What directX version is your graphics card" or something like that. I'll add this to my question. – SevenT2 Nov 17 '09 at 18:03

5 Answers5

1

I would set up a parsing function that reacts to every change in any of the form elements, and "compiles" the string containing the bits of information.

You could set this function to the "onchange" event of each element.

How the parsing function will have to look like is really up to what you want to achieve. It could be something like this: (intentionally leaving out framework specific functions)

function parse_form()
 {

  var age_string = "Your computer is"

  var age = document.getElementById("age").value;
  if (age == 1) age_string+= "one year old";
  if (age > 1) age_string+= "more than one year old";

... and so on.

 }
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 2
    Pekka, it is dangerous and bad style not to use the var keyword when introducing variables (because of implied globals). Explanations: http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript, http://javascript.crockford.com/code.html – Tom Bartel Nov 17 '09 at 18:05
0

A regular jQuery click listener will do. Making some assumptions about your HTML code (that you're using radio input buttons with label tags whose for attributes correspond to the IDs of the radio buttons):

$("#options input").click(function(){
    var optionID= $(this).attr(id);
    var outputText= $("label[for="+optionID+"]");
    $("#output").text(outputText);
});

This code will accomplish pretty much exactly what you want. If you want separate DIVs for each particular output message, just create divs each with class output with let's say a rel attribute corresponding to the ID of the input button, then:

$("#options input").click(function(){
    var optionID= $(this).attr(id);
    $("div.output[rel!="+optionID+"]").slideUp(function(){
        var outputDiv= $("div[rel="+optionID+"]");
    });
});

This code can be improved by better handling situations in which an already selected option is reclicked (try it... weird things may happen), or if clicks occur very quickly.

Alternately, you can just use the regular onclick attribute for the input radio buttons if you just want to stick to native Javascript. If you're interested in the appropriate code for that, post a comment, and someone will surely be able to help you out.

Steven
  • 17,796
  • 13
  • 66
  • 118
0

Here is an example with jQuery that will fit your needs.

// no tricks here this function will run as soon as the document is ready.
$(document).ready(function() {
    // select all three check boxes and subscribe to the click event.
    $("input[name=ComputerAge]").click(function(event) { 
        var display = $("#ComputerAgeTextDisplay"); // # means the ID of the element
        switch (parseInt(this.value)) // 'this' is the checkbox they clicked.
        {
            case 1:
                display.html("Your computer is one year old."); 
                break; 
            case 2:
                display.html("Your computer is two years old."); 
                break; 
            case 3:
                display.html("Your computer is three years old."); 
                break; 
            default:
                display.html("Please select the age of your computer."); 
                break;   
        }
    });
});
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
0
<span id='#options'>
  Options:
  <input type='radio' name='options' value='one'>One Year</input>
  <input type='radio' name='options' value='two'>Two Years</input>
  <input type='radio' name='options' value='three'>Three Years</input>
</span>
Output: "<span id='#output'></span>"

...

var labels = { 
  one: "Your computer is One Year Old",
  two: "Your computer is Two Years Old",
  three: "Your computer is Three Years Old"
};

$('#options :radio').click(function() {
   $('#output).html(labels[$(this).val()]);
});
Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
0

You'd need a way to associate each checkbox with the information you wanted to show. You could for example combine the name and value to get an ID to show, assuming the values are characters that are valid in an ID:

<input type="radio" name="age" value="1" /> One year
<input type="radio" name="age" value="2" /> Two years
<input type="radio" name="age" value="3" /> Three years

<div id="age-1"> Your computer is awesome!! </div>
<div id="age-2"> Your computer is so-so </div>
<div id="age-3"> Your computer sucks </div>

Now you just need a bit of script to tie them together:

function showElementsForInputs(inputs) {

    // Set the visibility of each element according to whether the corresponding
    // input is checked or not
    //
    function update() {
        for (var i= inputs.length; i-->0;) {
            var input= inputs[i];
            var element= document.getElementById(inputs[i].name+'-'+inputs[i].value);
            element.style.display= input.checked? 'block' : 'none';
        }
    }

    // Set the visibility at the beginning and also when any of the inputs are
    // clicked. (nb. use onclick not onchanged for better responsiveness in IE)
    //
    for (var i= inputs.length; i-->0;)
        inputs[i].onclick= update;
    update();

}

showElementsForInputs(document.getElementById('formid').elements.age);
bobince
  • 528,062
  • 107
  • 651
  • 834
  • for some reason that code doesn't work for me..? What do i need to put surrounding the script? – SevenT2 Nov 17 '09 at 18:22
  • ``. And put the script after the form with id `formid` and elements so that they're on the page to interact with when it runs. (Or alternatively, run `onload`.) – bobince Nov 17 '09 at 18:37
  • ah, silly me forgot to add the form tags :/ Is there anyway that i can make another set of checkboxes/radioboxes negate the displaying of one these divs, for a more relevant div. Thats what i'm ultimately after. For Example: RAM? [x] 128 MB [] 512 []1GB Videocard? [x]nvidia awesometron5000 []generic "Your videocard seems current but your ram seems to indicate that you are running an older motherboard" ...or something like that! – SevenT2 Nov 17 '09 at 18:44
  • You could display divs with `ids` like `age-1-memory-128`, and pass in combined input arrays covering all inputs with name `age` or `memory`. However, that assumes that you want to create a div for every combination of age and memory. If you have more complicated logical conditions like ‘display this if old else display that if new but poor memory else display the other...’ then you can't really use an automatic shortcut like this to tie individual inputs to divs. – bobince Nov 17 '09 at 19:23
  • Instead you would have to write specific logic in `update()` to pick the one element you wanted shown — or, probably easier by the time you're getting this complex, generate the content on the fly as in one of the other answers. – bobince Nov 17 '09 at 19:24
  • I've worked out a hacky way to do it. I will probably try and write the logic in the update() later on (when this inevitably fails) but for now i'm just embedding further questions after a question has been asked in the div that becomes visible, in that way, they can only answer one question at a time and i can still narrow it down. Will do for now! Thanks for your help, you can have the green ticky thing! :D – SevenT2 Nov 17 '09 at 19:45
  • Ah yes, that's a good idea: if a machine is so old the memory's always going to be tiny, no point in bothering to ask! – bobince Nov 17 '09 at 21:26