473,405 Members | 2,373 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,405 software developers and data experts.

Question about a select list? (code attached)

Hi all, let me begin by saying that I *ALMOST* have this complete!
What I'm trying to do is make it so my text area shows the innerHTML
of any select item with the same value. For example, if I have a
select list with 5 options:

<select name="select" onChange="changeValues();">
<option value="1">Option 1</option>
<option value="1">More stuff on option 1</option>
<option value="1">EVEN more option one stuff!!</option>
<option value="2">Option 2</option>
<option value="2">Some more option 2 stuff</option>
</select>

<textarea name="testimonialText" cols="45" rows="10">I'm merely a text
area.</textarea>

If a user selects option 1, then I would like all of the option 1
stuff to appear within the text area.

The code I have seems to *almost* be working properly, but if someone
could lend me a hand and point out what I'm doing wrong, it would be
VERY much appreciated.

Here's what I have so far:
function changeValues() {
var e = document.getElementById('select')
[document.getElementById('select').selectedIndex].innerHTML;
var en = e.replace("&nbsp;&nbsp;", '');
var s = en.replace("&amp;", '&');
//document.myform.testimonialText.value =s;

var myOptions;
for (myOptions=0; myOptions < window.document.myform.select.length;
myOptions++)
{
var se = document.getElementById('select')[myOptions].innerHTML;
if (window.document.myform.select.options[myOptions].value ==
window.document.myform.select.options[myOptions+1].value ) {
// alert(document.myform.select.options[myOptions].innerHTML);
document.myform.testimonialText.value =
document.getElementById('select')
[document.getElementById('select').selectedIndex].innerHTML;
} else {
//alert("no");
}
}
//alert(en);
}

Thank you for your help!
Jun 27 '08 #1
3 1952
On May 20, 8:57 am, Italio Novuas <geet.kartik...@gmail.comwrote:
Hi all, let me begin by saying that I *ALMOST* have this complete!
What I'm trying to do is make it so my text area shows the innerHTML
of any select item with the same value.
Don't use innerHTML, the content of an option element is plain text.
You get its value using the option element's text property.

For example, if I have a
select list with 5 options:

<select name="select" onChange="changeValues();">
<option value="1">Option 1</option>
<option value="1">More stuff on option 1</option>
<option value="1">EVEN more option one stuff!!</option>
<option value="2">Option 2</option>
<option value="2">Some more option 2 stuff</option>
</select>

<textarea name="testimonialText" cols="45" rows="10">I'm merely a text
area.</textarea>

If a user selects option 1, then I would like all of the option 1
stuff to appear within the text area.

The code I have seems to *almost* be working properly, but if someone
could lend me a hand and point out what I'm doing wrong, it would be
VERY much appreciated.

Here's what I have so far:
function changeValues() {
var e = document.getElementById('select')
It's not a good idea to give an element an ID that is the same as it's
tag name. Besides, you set its name attribute to select, not its ID.
IE thinks they're the same thing but other browsers know better.
Either add an ID attribute or use the name attribute explicitly.

[document.getElementById('select').selectedIndex].innerHTML;
That is very inefficient - store a reference to the select element and
re-use it. To get all the options with the same value, loop over the
select's options collection.

function changeValues() {
var sel = document.forms['myform'].elements['select'];
var value = sel.options[sel.selectedIndex].value;

Get the reference to the text area here too:

var ta = document.myform.testimonialText;
var en = e.replace("&nbsp;&nbsp;", '');
var s = en.replace("&amp;", '&');
I guess they do something useful in the "real" system.

//document.myform.testimonialText.value =s;

var myOptions;
for (myOptions=0; myOptions < window.document.myform.select.length;
myOptions++)
On every loop, the browser must resolve that reference and will get
exactly the same value every time - store it in a local variable.
Consider:

var opt;
for (var i=0, len=sel.options.length; i<len; i++)
{
var se = document.getElementById('select')[myOptions].innerHTML;
if (window.document.myform.select.options[myOptions].value ==
window.document.myform.select.options[myOptions+1].value ) {
// alert(document.myform.select.options[myOptions].innerHTML);
document.myform.testimonialText.value =
document.getElementById('select')
[document.getElementById('select').selectedIndex].innerHTML;
opt = sel.options[i];
if (opt.value == value) {
ta.value += opt.text;
Here's the whole function in one go:

function changeValues() {
var sel = document.forms['myform'].elements['select'];
var value = sel.options[sel.selectedIndex].value;
var ta = document.forms['myform'].elements['testimonialText'];
ta.value = '';

var opt;
for (var i=0, len=sel.options.length; i<len; i++) {
opt = sel.options[i];
if (opt.value == value) {
ta.value += opt.text;
}
}
}
--
Rob
Jun 27 '08 #2
Rob -
Thank you for your help. Your explaination was very clear and easy to
follow! I have rated you 5 stars!! Thanks so much!
Jun 27 '08 #3
RobG wrote:
On May 20, 8:57 am, Italio Novuas <geet.kartik...@gmail.comwrote:
>var e = document.getElementById('select')

It's not a good idea to give an element an ID that is the same as it's
tag name.
Why?
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Jun 27 '08 #4

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

Similar topics

2
by: Ray Todd Jr | last post by:
I have created form, query and report. In the form I have a Combo Box which pulls the search criteria (SaleStatus) from one of my tables. When I select a status and click on the search button...
3
by: Julia | last post by:
Thanks for all you responses assuming I have a collection of objects which I want to save in a database I wonder if the following is the way to deal with a situation that only some of the...
35
by: Stan Sainte-Rose | last post by:
Hi, What is the better way to save image into a database ? Just save the path into a field or save the image itself ? I have 20 000 images (~ 10/12 Ko per image ) to save. Stan
11
by: Madison Kelly | last post by:
Hi all, I am new to the list and I didn't want to seem rude at all so I wanted to ask if this was okay first. I have a program I have written in perl which uses a postgresSQL database as the...
4
by: Doug Handler | last post by:
Ok, I think this is my last one - in my app, the user can select via a dialog box the dll's they want to load. I use a checkbox to track this (no worries there), but, once a dll has been bound,...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
16
by: Richard Maher | last post by:
Hi, I have this Applet-hosted Socket connection to my server and in an ONevent/function I am retrieving all these lovely rows from the server and inserting them into the Select-List. (The on...
3
by: maximisedk | last post by:
I need to create a HTML table for a number of users. Each user will have a unique HTML table in regard to number of columns, as the user can choose to append columns to his HTML table. The way I...
6
by: woodey2002 | last post by:
Hi Everyone. Thanks for your time. I am trying to create a search form that will allow users to select criteria from multiple multi select boxes. So far i have managed to achieve a search option...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.