views:

191

answers:

1

Hi,

I have this code and I keep getting undefined if I test the selectedIndex.

alert(x.selectedIndex);

So, setting it is also a problem.

Does anyone possibly see what the problem is?

//makes list off tags
function ttyps_select(data,naamsel,selectid, containerid){

if(!ttyps.length){
    jQuery.each(data, function(index, itemData) {  
        ttyps.push( new Tagtype(itemData.tag_id, itemData.tag ));
    });
}
opties = "<option value=\"-1\"></option>\n"; 
for(var i=0; i<ttyps.length; i++) {

    var dfnkey = ttyps[i].tag_id;
    var dfnsel = ttyps[i].tag;

    if (dfnkey==selectid) {
        opties +="<option value="+ttyps[i].tag_id+" SELECTED>"+dfnsel+"</option>\n";

    } else {
        opties +="<option value="+dfnkey+">"+dfnsel+"</option>\n";
    }
}

 $("<select name=\"" + naamsel + "\" size=\"1\" ></select>") 
        .html(opties)
        .change(function(e){
            select_tag(containerid);
        })
        .appendTo("#"+naamsel); 
}

function select_tag(id) {
    var x = $('#frmttypid'+id+' select');
    var ttidx = x.val();
    var tag = getTagtype(ttidx).tag;
    x.selectedIndex=0;
    x.blur();
     if( tag ){
        document.forms['frmtags']['frmtag'+id].value=tag;
     } 
}

thanks, Richard

+1  A: 

$('selector') (jQuery) returns an object with array-like collection of matched DOM nodes. Your x variable is an jQuery object, not a reference to any particular <select/> element. use

x[0].selectedIndex

x[0] is a reference to the first DOM node in the jQuery object.

pawel
thanks, I already found the solution by using document.getElementById(); because I had not figured out that the jquery selector always returns an array like collection. So yours is better and I will change the code.
Richard