JavaScript Decision Tree tool

My goal has been to make a JavaScript Decision Tree tool as a new content module for our next site redesign.

It needs to work such that the same piece of JavaScript can be utilised without modification for any decision tree tool we build on the site, with a very simply-defined markup structure,

So I’ve been self-administering repeated doses of headdesk/facepalm over the last couple of days, but I came back today after a Friday lunch (you know what I mean) and found a solution. It’s not pretty, with at least two nested ‘for’ loops, but it works.

I’ve created a JSFiddle of it, so you can take a look at it, modify it, laugh at it, etc.
Open it in JSFiddle or see it embedded below:

This entry was posted in Coding, JavaScript. Bookmark the permalink.

6 Responses to JavaScript Decision Tree tool

  1. JP says:

    js fiddle . . . sounds dodgy . . .

  2. Adam Bright says:

    Awesome job.

    I did add something for my purposes, because the nodes were getting long, FYI.

    
                fieldsets.addClass('hide');
                $('fieldset.option0').removeClass('hide');
                for (i = 0; i < options.length; i += 1) {
                    options_buffer += options[i];
                    $('fieldset.option' + options_buffer).removeClass('hide');				
    				document.getElementById('question' + options_buffer.replace(/(.)/g, '_$1')).scrollIntoView(); //***NEW Scroll to the latest option.***
                }
    

  3. Dan says:

    Nice touch! Cheers!

  4. Johanus says:

    I found a small bug/limitation, whatever you would like to call it. If you add 10+ radio buttons in my example 11, 12 were showing up twice because the same class names were being used/generated for options 1, 2 for another step were they both automatically worked out to ‘option211′ and ‘option212′. But otherwise a pretty nice script.

  5. Nina says:

    I pay a quick visit daily some blogs and sites to read articles or reviews, but this web site gives
    feature based posts.

    Here is my webpage :: online guitar lessons
    (Nina)

  6. Christal says:

    Why visitors still usse to read news papers when inn this technological world
    eveeything is presented on net?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>