Solved

DOM - JavaScript Changing selected option in select

Posted on 2007-12-05
6
12,381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Most of the sites are being standardized with W3C Web Standards. W3C provides lot of web standard services to the web. They have the web specification, process and documentation for all the web standards. You can apply HTML, CSS and Accessibility st…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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