JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 20th, 2005, 08:19 AM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
Send a message via ICQ to Narek
<select> innerHTML problem

Hi everyone,

Ok, what I'm trying to do is use the innetHTML property of a <secelct> box to change it's contents. What I do is generate a string with a list of <option> tags and then use

document.getElementById("mybox").innerHTML = s;

to fill the select box.

Now it doesn't work, particularily, the contents of the select box are displayed in a single string. When it should have been 3 items like
item1
item2
item3

it's really one item showing in the drop down menu:
item1item2item3

Now I looked at the contents of the string before putting it into the innerHTML and at the innerHTML after assigning the string to it. It looks pretty wierd. The string at first seems right
<option value=1>item1</option><option value=2>item2</option>

but after inserting the innerHTML contains something like

item1</option><option value=2>item2</option>

lacking the first opening option tag.

Does anyone have an idea as to what's the problem here and how it can be fixed?
__________________
You'll live, only the best get killed.
--Charles De-Gaulle

Reply With Quote
  #2  
Old September 20th, 2005, 08:24 AM
diek diek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Posts: 29 diek User rank is Corporal (100 - 500 Reputation Level)diek User rank is Corporal (100 - 500 Reputation Level)diek User rank is Corporal (100 - 500 Reputation Level)diek User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 14 h 44 m 40 sec
Reputation Power: 0
First thing's first... innerHTML wasn't designed to do what you're trying to do. innerHTML should really only be used as a last resort when DOM manipulation gets too cumbersome. Select elements have an add() and remove() method available to manipulate elements and you can always edit elements directly with selectElement.options[index].text and selectElement.options[index].value. I use select boxes extensively in a lot of my projects and this has always been the cleanest way to go.

Reply With Quote
  #3  
Old September 20th, 2005, 02:30 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
I agree, innerHTML should not be used for this.

Take a look at this tutorial.
__________________
Spreading knowledge, one newbie at a time. I'm available for hire at Dynamic Site Solutions.

Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

Remember people spend most of their time on other people's sites (so don't violate web design conventions).

Reply With Quote
  #4  
Old September 21st, 2005, 08:16 AM
Narek Narek is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Yerevan, Armenia
Posts: 224 Narek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 h 1 m 30 sec
Reputation Power: 11
Send a message via ICQ to Narek
Gee guys, i've been using innerHTML for almost everything It looked very straightforwrd to me

Anyway, thanks for the help

Reply With Quote
  #5  
Old September 21st, 2005, 08:21 AM
briancaspe briancaspe is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 20 briancaspe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 h 54 m 29 sec
Reputation Power: 0
you might be able to replace the entire select box with innerHTML for whatever the select's parent element is...just a guess.

Reply With Quote
  #6  
Old September 21st, 2005, 04:34 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 30th Plane (19500 - 19999 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 19,835 Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 48th Grade (Above 100000 Reputation Level) 
Time spent in forums: 6 Months 1 Day 22 h 11 m
Reputation Power: 4192
Using innerHTML to create or modify form controls will not work well cross-browser.

I hope you looked at the link in my previous reply.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > <select> innerHTML problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap