user2415131
user2415131

Reputation: 95

passing data into select options then into a function, jquery .data();

I have a form that once the first drop down is selected the next will be populated by children of the first. I need to pass the "branch" that has been selected. The problem is I need to pass something other than a string to the function that populates the next select's options. I am trying to use jQuery's .data(); method but I keep getting an error.

function Branch ( name, level, parent ) {
    this.name = name;
    this.level = level;
    this.children = [];
    this.parent = parent;
}

function makeNextOptions ( branch ){
    var b = branch;
    while  ( b ) {
        if ( b.hasChildren() ) {
            f.append( $("<select id="+l2+"></select>" ) );
            var c = b.children;
            j = c.length;
            $("#"+l2 ).append($("<option></option>")
                              .attr("value", 2)//.id )
                              .text( "text" ) 
                             );
            while ( j-- ) {
                $("#"+l2 ).append($("<option></option>")
                                  .attr("value", c[j].name)//.id )
                                  .text( c[j].name)
                                  .data( c[j] ); 
                                 )};

            $( "#"+l2 ).change( function () {
                var sel = $( "select option:selected" ).data();
                console.log(sel);
                createLevel( sel )
            })

            l--;
            n = n.parent;
        }else
        { n= null;
        }
    }    

}

But I keep getting an error that ( "sel" )Object # has no method 'hasChildren'. But is does, because "sel" should just be another Branch.

Upvotes: 0

Views: 183

Answers (1)

Alex Funk
Alex Funk

Reputation: 435

You can use jquery's function .data(key, value) which you are able to pass objects.

Upvotes: 1

Related Questions