?
Solved

DOM - JavaScript Changing selected option in select

Posted on 2007-12-05
6
Medium Priority
?
12,384 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 1500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

770 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