473,322 Members | 1,259 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,322 software developers and data experts.

Using element variables

Hello all!

here is my code:

<script>
function showMe(group){

alert(bin51.bin51.value) 'this line returns selected option just fine

'this is the line I need help with
alert(document.getElementsById(group).value)

'I would like it to convert the group variable into
'bin51 and make it return the selected option as the line above
}
</script>

<body>
<form name = "bin51">
<select name = "bin51">
<option value="bin5102">Bin5102 - 500pc - $1000</option>
<option value="bin5103">Bin5103 - 250pc - $650</option>
</select>
<button onclick="showMe('bin51')">Add to Cart</button>
</form>
</body>
Please keep in mind this is not the complete code. Some of the elements in
this script are generated by ASP and represent one of many forms on the
products page.

TIA for any suggestions.
~Michael

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #1
4 1313
Michael Mishler wrote:

[...]

<script>
function showMe(group){

alert(bin51.bin51.value) 'this line returns selected option just fine
It shouldn't, except in IE perhaps.


'this is the line I need help with
alert(document.getElementsById(group).value)
group="bin51"
x=document.forms[group].elements[group];
alert(x.options[x.selectedIndex].value);

Mick
'I would like it to convert the group variable into
'bin51 and make it return the selected option as the line above
}
</script>
<form name = "bin51">
<select name = "bin51">
<option value="bin5102">Bin5102 - 500pc - $1000</option>
<option value="bin5103">Bin5103 - 250pc - $650</option>
</select>
<button onclick="showMe('bin51')">Add to Cart</button>
</form>

[...]
Mick
Jul 23 '05 #2
Thank you. Worked great.

Michael
"Mick White" <mw***********@rochester.rr.com> wrote in message
news:CV*******************@twister.nyroc.rr.com...
Michael Mishler wrote:

[...]

<script>
function showMe(group){

alert(bin51.bin51.value) 'this line returns selected option just fine


It shouldn't, except in IE perhaps.


'this is the line I need help with
alert(document.getElementsById(group).value)


group="bin51"
x=document.forms[group].elements[group];
alert(x.options[x.selectedIndex].value);

Mick

'I would like it to convert the group variable into
'bin51 and make it return the selected option as the line above
}
</script>

<form name = "bin51">
<select name = "bin51">
<option value="bin5102">Bin5102 - 500pc - $1000</option>
<option value="bin5103">Bin5103 - 250pc - $650</option>
</select>
<button onclick="showMe('bin51')">Add to Cart</button>
</form>

[...]
Mick


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Jul 23 '05 #3
"Michael Mishler" <no*****@nohow.com> writes:
here is my code:

<script>
Should be
<script type="text/javascript">
function showMe(group){

alert(bin51.bin51.value) 'this line returns selected option just fine
I assume the "'" and text following it is *not* part of your code.

This only works in some browser settings of some browsers. When it works,
it is equivalent to:
alert(document.forms['bin51'].elements['bin51'].value);
This other form is longer, but it both works in all current browsers and
is W3C DOM standard compliant.
'this is the line I need help with
alert(document.getElementsById(group).value)
Two problems. It's "getElementById" (singular), and your document
has no elements with an "id" attribute, so it shouldn't work at
all. Names and id's are not the same, and in a standards compliant
browser, getElementById should only work in id's.
<body>
<form name = "bin51">
<select name = "bin51">
Add an "id" attribute:
<select name="bin51" id="bin51ID">
<option value="bin5102">Bin5102 - 500pc - $1000</option>
<option value="bin5103">Bin5103 - 250pc - $650</option>
</select>
<button onclick="showMe('bin51')">Add to Cart</button>

and use the Id:
showMe('bin51ID');

/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 23 '05 #4
Lasse Reichstein Nielsen wrote:

[Nearly fullquote intended]
"Michael Mishler" <no*****@nohow.com> writes:
[...]
function showMe(group){

alert(bin51.bin51.value) 'this line returns selected option just fine


I assume the "'" and text following it is *not* part of your code.

This only works in some browser settings of some browsers. When it works,
it is equivalent to:
alert(document.forms['bin51'].elements['bin51'].value);
This other form is longer, but it both works in all current browsers and
is W3C DOM standard compliant.
'this is the line I need help with
alert(document.getElementsById(group).value)


Two problems. It's "getElementById" (singular), and your document
has no elements with an "id" attribute, so it shouldn't work at
all. Names and id's are not the same, and in a standards compliant
browser, getElementById should only work in id's.
<body>
<form name = "bin51">
The `action' attribute is required for the `form' element.
<select name = "bin51">


Add an "id" attribute:
<select name="bin51" id="bin51ID">


No need to have different `name' and `id' attribute values here.
No need to have an ID here at all.
<option value="bin5102">Bin5102 - 500pc - $1000</option>
<option value="bin5103">Bin5103 - 250pc - $650</option>
</select>
<button onclick="showMe('bin51')">Add to Cart</button>

and use the Id:
showMe('bin51ID');


As he is using a `form' element, he should not rely on not backwards
compatible document.getElementById() at all and he should not be
advised so.

Instead, he should use

showMe(this.form.elements['bin51']);

and update the showMe() method accordingly, using the passed object
reference. This will work in many (all?) JS capable UAs, including,
but not limited to, standard compliant ones as it originates from
DOM Level 0 (IE3/NN3).
PointedEars
--
Let us not judge others because of their religion, color or nationality.
We are all just human beings living together on this planet. (poehoe.de)
Jul 23 '05 #5

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

Similar topics

4
by: marc.wyburn | last post by:
Hi, I am trying to move away from Windows only scripting to Python. I've written a quick script that will pull the product version from the client registry. I can get the IP addresses from a file...
7
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the...
9
by: Charley Kyd | last post by:
I'm a newbie who needs advice about how to use external files of JavaScript code. I spent an hour this afternoon browsing through JavaScript books at the local book store. In about 15 different...
3
by: Sugapablo | last post by:
I would like to use variables in... for(q = 0; q < i; q++) { if(document.varArray1.varArray2.checked == TRUE) { alert("YES"); } else { alert("NO"); } }
15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
8
by: Sergio Otoya | last post by:
Hi all, I need to add an input hidden field to an existing form (post). I have tried a couple things like adding the '<INPUT type=hidden name=idSelectedURL value=http://server/documents>' to...
17
by: Davíð Þórisson | last post by:
now in my web I have some global variables to be used in many different subpages, in the old ASP I simply loaded a variables.asp file into memory using the eval() function. Now I'd like to use XML...
14
by: hgraham | last post by:
Hi, I'm trying to understand how to work the dom, and all I'm trying to do is insert a link right before another link in the html based on it's href value. This isn't a real world example - I'm...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
4
by: David Schwartz | last post by:
So, I'm having a bit of a time trying to insert an <imgtag into an <atag using an xsl variable. Consider the following template. The contents of the foo variable doesn't show up in the anchor. When...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.