Question

I can't seem to change the text of the first option element of a drop down list. I've tried:

var e_option = document.getElementsByTagName("option");
e_option[0].text = "Selectionnaire";

and

var e_option = document.getElementsByTagName("option");
e_option[0].innerHTML = "Selectionnaire";

and

var e_option = document.getElementsByTagName("option");
e_option[0].innerText = "Selectionnaire";

However, nothing seems to change the option's text.

The strangest thing is that:

    var e_option = document.getElementsByTagName("option");
    e_option[0].style.color = "red"; 

does change the option to have red coloured font, and

    var e_option = document.getElementsByTagName("option");
    alert(e_option[0].text);

does alert/popup the current text of the option.

This is the HTML:

    ...
    <td id="control">
        <SPAN CLASS="rsfieldcontrol"><INPUT TYPE=TEXT CLASS="rsfieldcontrol" MAXLENGTH=20 SIZE=38 TABINDEX="70" NAME="CONTACTVERIFY1businessPostalCode" VALUE=""></SPAN>
    </td>
    <td>&nbsp;

    </td>
    <td>
        <SPAN CLASS="rsfieldlabel">Pays :</SPAN>
    </td>
    <td id="control">    
        <SPAN CLASS="rsfieldcontrol">
             <SELECT  CLASS="rsfieldcontrol"  TABINDEX="110" NAME="CONTACTVERIFY1businessCountry">
              <OPTION LABEL=" - Select an option - " VALUE=" - Select an option - "> - Select an option - </OPTION>
              <OPTION LABEL="Afghanistan" VALUE="Afghanistan">Afghanistan</OPTION><OPTION LABEL="Albania" VALUE="Albania">Albania</OPTION>
              //I cut out the other 200 odd countries here
              <OPTION LABEL="Zimbabwe" VALUE="Zimbabwe">Zimbabwe</OPTION></SELECT></SPAN> 
    </td>
</tr>
<tr>
    <td width="30px">&nbsp;

    </td>
    <td style="font-weight:bold;">
        <SPAN CLASS="rsfieldlabelrequired">* Courriel :</SPAN>
    </td>
      ...

Please note that all select, option and span tags are generated by LexisNexis InterAction, and I cannot change the code. This is why I'm using javascript to change the first option from English to French.

Any help would be appreciated!!

Was it helpful?

Solution

Woops!

The value of the label attribute of the option tag is being displayed instead of the text between the option opening and closing tags. Therefore I just need to write:

var e_option = document.getElementsByTagName("option");
e_option[0].setAttribute("label", "");
e_option[0].text = " - Selectionner - ";

or even

var e_option = document.getElementsByTagName("option");
e_option[0].setAttribute("label", " - Selectionner - ");

Thanks for your comments and help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top