Solved

DOM - JavaScript Changing selected option in select

Posted on 2007-12-05
6
12,377 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will learn how to dynamically set the form action using jQuery.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

821 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