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.

5 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. Robert says:

    Great beat ! I wish tto apprentice while you amend yokur
    website, how can i subscrijbe for a blog website? The accfount helped mee a acceptable deal.
    I had been a little bit acquainted of this
    your broadcast offered bright clear idea

Leave a Reply

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