Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DOM - JavaScript Changing selected option in select

Posted on 2007-12-05
6
Medium Priority
?
12,385 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

610 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