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

Select Box Woes

GazMathias
228 Expert 128KB
Hi Guys.

Hopefully someone can guide me with this, its driving me nuts!

Below is code from a little test I put together to test theory, in the real site, however, this will be looped from a database.

My problem, is that if I select, say, Option 8 from the second select box and click submit_2, the function picks up the third index from the first select box and so returns "Option3"

Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.  
  3.     function AddItem(theid) {
  4.  
  5.         var x=document.getElementById("cboProduct_" + theid).selectedIndex
  6.         var y=document.getElementsByTagName("option")
  7.         var code=y[x].value
  8.  
  9.         var qty = document.getElementById("txtQty_" + theid).value;
  10.  
  11.         alert(code)
  12.     }
  13.  
  14. </script>
  15.  
  16. Code:<select name="cboProduct_1" id ="cboProduct_1">
  17. <option selected value ="None">Select an option</option>
  18. <option value="Option1">Option 1</option>
  19. <option value="Option2">Option 2</option>
  20. <option value="Option3">Option 3</option>
  21. <option value="Option4">Option 4</option>
  22. <option value="Option5">Option 5</option>
  23. </select> Qty:<input type = "text" size="5" id="txtQty_1">
  24.  
  25. <input type="submit" value = "Add Item" onClick="AddItem('1')" name="submit_2" id="submit_2">
  26.  
  27. <br /> <br />
  28.  
  29. Code:<select name="cboProduct_2" id="cboProduct_2">
  30. <option selected value ="oneN">Select an option</option>
  31. <option value="Option6">Option 6</option>
  32. <option value="Option7">Option 7</option>
  33. <option value="Option8">Option 8</option>
  34. <option value="Option9">Option 9</option>
  35. <option value="Option10">Option 10</option>
  36. </select> Qty:
  37. <input type = "text" size="5" id="txtQty_2">
  38.  
  39. <input type="submit" value = "Add Item" onClick="AddItem('2')" name="submit_2" id="submit_2">
  40.  
  41. And so on.....
  42.  
Anyone know how to fix this?

Suggestions of alternate approaches are also welcome.

Gaz.
May 29 '09 #1
6 1418
acoder
16,027 Expert Mod 8TB
You can either use:
Expand|Select|Wrap|Line Numbers
  1. selObj.options[selObj.selectedIndex]
  2. // or if you must use getElementsByTagName
  3. selObj.getElementsByTagName("option")[selObj.selectedIndex]
where selObj is a reference to the select box.
May 29 '09 #2
Frinavale
9,735 Expert Mod 8TB
@acoder
Is there an Or to your either acoder ?
May 29 '09 #3
GazMathias
228 Expert 128KB
Thanks acoder, works like a charm. I was trying to do this yesterday but was using:

Expand|Select|Wrap|Line Numbers
  1. theid.options[theid.selectedIndex]
  2.  
Where theid contained the full name of the element, seems obvious now!
May 29 '09 #4
acoder
16,027 Expert Mod 8TB
@Frinavale
Yes, in the code :) Supposed to be two sets of code tags, but I was lazy!
May 29 '09 #5
acoder
16,027 Expert Mod 8TB
@GazMathias
No problem.
I was trying to do this yesterday but was using:

Expand|Select|Wrap|Line Numbers
  1. theid.options[theid.selectedIndex]
  2.  
Where theid contained the full name of the element, seems obvious now!
That might've worked in IE, but it's obviously incorrect. Another option is to get the value directly:
Expand|Select|Wrap|Line Numbers
  1. selObj.value
but you need to make sure you have the values of the options set for it to work in IE.
May 29 '09 #6
omerbutt
638 512MB
@GazMathias
another is to make the form object and access through that obj you can either pass that obj through a function assigned to an onclick event on a button or make it manually in the function
lets say you form name is myform so
Expand|Select|Wrap|Line Numbers
  1. var fobj=document.forms['myform'];
  2. fobj.elements['name'].options[fobj.elements['name'].selectedIndex].value;
  3.  
May 30 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Mark | last post by:
O, woe is me, to have seen what I have seen, see what I see! (That's Shakespeare for those who were wondering what I'm on about) I am "having fun" with cookies. And I wonder if I have...
0
by: Cedric | last post by:
This is a 3 weeks old problem, but having found a solution (and having looked for one here, finding only this message), I'm replying now. From: Jive (someone@microsoft.com) Subject: Upgrade...
3
by: Angel Cat | last post by:
Trying to get my jobs to send mail when job fails. Should be easy but it's giving me headache Had a whole slew of issues. Outlook is installed with a n outlook mail profile set up that can...
2
by: Andrew Thompson | last post by:
- NN 4.78 rendering woes, links at far left - I am trying to rework an old site, make it valid html and css, improving the x-browser and 'older browser' compatibility. My efforts so far, have...
0
by: Arun Bhalla | last post by:
I'm having some inconsistency problems with my deployment project ("Setup") and its custom actions ("Installer"). I'm using Visual Studio .NET 2003 (.NET 1.1, no service pack) on Windows XPSP1. ...
9
by: Mark Rae | last post by:
Hi, This time, I'm looking for a regular expression which says "the string must contain exactly seven or exactly eight digits" e.g. 123456 fails 1234567 passes 12345678 passes 123456789...
1
by: hdogg | last post by:
Scope Woes - IF statement nested in WHILE statement -PHP I have an array $actuals_sum. <?php while(conditions) { if($i == '24) {
4
by: =?Utf-8?B?VkIgSm9ubmll?= | last post by:
I am at my witless end here, please help! I have an ASP.Net aspx web page, hosted on Windows Server 2003, that receives a query string with the path to an autocad drawing file selected from a...
1
by: Mike Howarth | last post by:
I've been having a few problems with connecting to SQL Server, initially I was using dblib however found some problems with returning text fields whereby all text fields were ignored and it bawked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.