Solved

DOM - JavaScript Changing selected option in select

Posted on 2007-12-05
6
12,370 Views
Last Modified: 2013-11-19
The following JavaScript convert something like

<span id="container">
    <span id="data">
        some data
    </span>
</span>

to
<span id="container">
    <select id="data" name="selectName">
        <option value="defined by user">defined by user</option>
        <option value="defined by user">defined by user</option>
    </select>
</span>

My problem is after making the options elements, I want to select the one who fit nodeText... It works in Firefox but not in IE... HOW DO I CHANGE THE SELECTION IN IE !!! ???
function TransformToSelect(nodeName, selectName, selectId, options)

{

    var nodeToRemove = document.getElementById(nodeName);

    var nodeText = nodeToRemove.textContent;

    var parentNode = nodeToRemove.parentNode;
 

    if (nodeToRemove != null && parentNode != null)

    {

        RemoveNode(nodeName);
 

        var container = document.createElement("span");

        container.id = nodeName;
 

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

        select.name = selectName;

        select.id = selectId;
 

        var selIndex = 0;
 

        for (var i = 0; i < options.length; i++)

        {

            var currentOption = document.createElement("option");
 

            try

            {

                select.add(currentOption); //IE

            }

            catch (ex)

            {

                select.add(currentOption, null); //Firefox et autres

            }

            

            currentOption.value = options[i][0];

            currentOption.text = options[i][1];

            if (currentOption.text == nodeText)

                currentOption.selected = true;

        }
 

        parentNode.appendChild(container);

        container.appendChild(select);

    }    

}

Open in new window

0
Comment
Question by:mychel_normandeau
  • 3
  • 3
6 Comments
 

Author Comment

by:mychel_normandeau
ID: 20414717
The problem comes from:

var nodeText = nodeToRemove.textContent;

which works in FF but not in IE... What is the IE equivalent of textContent ? innerHTML does not suit me since I can have things like <b> or <i> into innerHTML...
0
 
LVL 12

Accepted Solution

by:
Sinoj Sebastian earned 500 total points
ID: 20414808
>> What is the IE equivalent of textContent ?
for IE "innerText"
0
 

Author Comment

by:mychel_normandeau
ID: 20414841
Yeah, I figured that out... My final solution is:

    var nodeText;
    if (document.all)
       nodeText = nodeToRemove.innerText; //IE
    else
       nodeText = nodeToRemove.textContent; //Firefox et autres
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 20414851
try it like this

 var nodeText  = ""
if(document.all){
   nodeText  = nodeToRemove.innerText ;
} else{
   nodeText = nodeToRemove.textContent;
}
0
 
LVL 12

Expert Comment

by:Sinoj Sebastian
ID: 20414856
wow..
The same solution :)
0
 

Author Comment

by:mychel_normandeau
ID: 20415547
Hehe yes :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now