473,387 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple innerHTML issue

I'm trying to dynamically change the contents of a select box by doing the
following...

function myfunc() {
var obj=document.getElementById("objname"); // name of the select box
var str='';
str+='<option>blah</option>';
obj.innerHTML=str;
alert(obj.innerHTML);
}

For reasons I don't understand, the alert gives me 'blah</option>'. Where is
the leading <option> tag?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 20 '05 #1
5 10109
I'm not sure you ammend select objects like this......i.e whether it works
in various browsers.
I'd strip it out and create 'new Option'

Someone else may have a suggestion, but I am not sure this is the right
route for modifying options myself.....I could be wrong.

Stu
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bn**********@chessie.cirr.com...
I'm trying to dynamically change the contents of a select box by doing the
following...

function myfunc() {
var obj=document.getElementById("objname"); // name of the select box
var str='';
str+='<option>blah</option>';
obj.innerHTML=str;
alert(obj.innerHTML);
}

For reasons I don't understand, the alert gives me 'blah</option>'. Where is the leading <option> tag?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Jul 20 '05 #2
This works:

<html>
<head>
<title>Dynamically Add Option to a Select Menu</title>
<script type="text/javascript">
function addOption() {
var oSelect = document.getElementById( 'myselect' )
nextOptIndex = oSelect.length + 1
newOpt = document.createElement( 'OPTION' );
newOpt.text = "New Option #" + nextOptIndex
newOpt.value = nextOptIndex
oSelect.add( newOpt, nextOptIndex )
}
</script>
</head>
<body>
<h1>Dynamically add an option to a select menu</h1>
<select name="myselect" id="myselect">
<option value="default">choose me</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="button" value="add" name="add" onClick="addOption()">
</body>
</html>

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bn**********@chessie.cirr.com...
I'm trying to dynamically change the contents of a select box by doing the
following...

function myfunc() {
var obj=document.getElementById("objname"); // name of the select box
var str='';
str+='<option>blah</option>';
obj.innerHTML=str;
alert(obj.innerHTML);
}

For reasons I don't understand, the alert gives me 'blah</option>'. Where is the leading <option> tag?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Jul 20 '05 #3
DB McGee <no*****@noreply.com> spoke thus:
<title>Dynamically Add Option to a Select Menu</title>
(snipped)


Thanks - I think I'm undestanding why what I had didn't work. The options are
an array and I wasn't treating them like one...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 20 '05 #4
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
I'm trying to dynamically change the contents of a select box by doing the
following...


Since you don't say which browser you are using, I'll assume it is
iCab on a Macintosh.

Just kidding, I'll assume it is IE on Windows, since that is what it
usually is when people don't say.

Don't use innerHTML to change the content of a select element in IE.
The content of a select element isn't general HTML, and it won't work
as you expect it to. Using innerHTML adds child nodes to the select
node in the DOM tree, not options to the options collection.

Use
selectRef.add(new Option("text","value"))
instead.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Hi Stuart,

1) document.getElementById is not crossBrowser compatible -- it will
be recognized only by version 5+ browsers.

2) if you want to change a specific option's value/text, the following
is sufficient:
function doit(selObj, ndx)
{
txt = 'a'
selObj.options[ndx].text = txt;
selObj.options[ndx].value = txt;
}

<form name='a'>
<select name="b" onchange="alert(this.options[this.selectedIndex].value)">
<option value='0'>1</option>
<option value='1'>2</option>
<option value='2'>3</option>
</select>
<input type="button" onclick="doit(this.form.b, 2)">
</form>

3) if you want to remove an option, you either set that index to null
selObj.options[0] = null
or set the options.length to some number
selObj.options.length = 0;

4) if you wish to append new options, you need to use the new Option()
constructor. see the selection list script/tutorials at my GrassBlade
Javascript site: http://members.aol.com/grassblad

Vinny

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bn**********@chessie.cirr.com...
I'm trying to dynamically change the contents of a select box by doing the
following...

function myfunc() {
var obj=document.getElementById("objname"); // name of the select box
var str='';
str+='<option>blah</option>';
obj.innerHTML=str;
alert(obj.innerHTML);
}

For reasons I don't understand, the alert gives me 'blah</option>'. Where

is
the leading <option> tag?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: KK | last post by:
Please help! I am currently experiencing a bug in Safari v125.9. When I modify the value of form input box and then get the innerHTML property of the surrounding div object - I am returned the...
6
by: Andrew Poulos | last post by:
Given that I need to be able to add a TYPE attribute when I'm using createElement and it seems to fail in both IE and FF (but not MZ) is it 'safer' to use innerHTML instead? I can dynamically...
9
by: Astra | last post by:
Hi everybody Wonder if you could help me out. I created a simple JavaScript routine to enable a user to click backwards and forwards between small news articles. This routine works fine in IE...
5
by: W.Sh | last post by:
Hello Everyone! I'm having some issues with javascript that I can't seem to resolve... Basically, I have a very simple code that's supposed to change the innerHTML of a span element whenever I...
2
by: sveinn | last post by:
Hi all, I've read through this group searching for an answear about this problem. Few have come close but not quite what I need. My problem is this: I'm using Ajax to fetch a new table with...
6
by: sonic | last post by:
Ok, i am sure everyone is sick of hearing about this. but i've checked about 10 different posts/sites about this issue, and they all say "use DOM" but i think there is more to be said. Perhaps I...
7
by: WSPL5 | last post by:
I have done this function before and I cannot figure out why it is not working. I am receiving the results of an xmlhttp call and I am looping through the values I am creating links with an OnClick. ...
6
by: PaPa | last post by:
I'm not sure this is a javascript issue or an HTML issue. I notice that when I extract the contents of a div using the innerHTML property (?), that I wind up with a literal variable (?) which...
2
by: andersond | last post by:
Most of the code below is just included so I could use a cut and paste to assure that everything was being presented completely. The script executes perfectly except for the line where it is to set...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.