Connecting Tech Pros Worldwide Forums | Help | Site Map

Setting "text" value to "option" element in IE

gabon
Guest
 
Posts: n/a
#1: Nov 23 '05
I'm creating a select entirely through JavaScript and very strangely IE
doesn't show the text in the option elements.

Here part of the code:

this.form_country=document.createElement("select") ;
var option;
for(var i=0; i<arr.length; i++){
option=document.createElement("option");
option.value=arr[i].code;
option.text=arr[i]._value;
alert(option.value+" - "+option.text);
this.form_country.appendChild(option);
}

It works perfeclty on FireFox, funny thing is that the alert works
properly but there is no way to see any text inside :S

Any idea? Of course I googled and very strangely I didn't find anything
about it.


Many thanks, chr


RobG
Guest
 
Posts: n/a
#2: Nov 23 '05

re: Setting "text" value to "option" element in IE


gabon wrote:[color=blue]
> I'm creating a select entirely through JavaScript and very strangely IE
> doesn't show the text in the option elements.
>
> Here part of the code:
>
> this.form_country=document.createElement("select") ;[/color]

I'll use this for convenience, you may chose to do so, or not:

var sel = document.createElement("select");

[color=blue]
> var option;
> for(var i=0; i<arr.length; i++){[/color]

Replace from here...
[color=blue]
> option=document.createElement("option");
> option.value=arr[i].code;
> option.text=arr[i]._value;
> alert(option.value+" - "+option.text);
> this.form_country.appendChild(option);[/color]

.... to here with:

sel.options[sel.options.length] =
new Option(arr[i]._value, arr[i].code);


Change 'sel' back to 'this.form_country' if you want.
[color=blue]
> }
>
> It works perfeclty on FireFox, funny thing is that the alert works
> properly but there is no way to see any text inside :S
>
> Any idea? Of course I googled and very strangely I didn't find anything
> about it.[/color]

Syntax of new Option is:

new Option([text[, value[, defaultSelected[, selected]]]])

where defaultSelected & selected are booleans (true/false).


This is an old reference, but all I could find in a hurry (sleep
beckons...):

<URL:http://devedge-temp.mozilla.org/library/manuals/2000/javascript/1.3/reference/option.html>



--
Rob
gabon
Guest
 
Posts: n/a
#3: Nov 23 '05

re: Setting "text" value to "option" element in IE


Thanks a lot Rob, it's what I needed :)


Nice one, chr

Closed Thread


Similar JavaScript / Ajax / DHTML bytes