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

pass option value as variable for javascript function

Folks,

I have a drop-down select menu generated server-side. I was hoping to
pass more than a single variable from the option value to a javascript
function. Is this possible? At the moment, my option values consist of three
numbers separated by commas. I thought I could pass these three numbers as
three distinct variables to the javascript function, but it obviously
doesn't work this way.

e.g.

<select name="selectname">
<option value="1,2,3">first selection</option>
etc.
</select>

<input type=button
onclick="dothis(document.form.selectname.options[document.form.selectname.selectedIndex].value)">

can't evidently be used as

<script lang="javascript">
function dothis(a,b,c) {
etc.
}
</script>

Any suggestions?

Dave
--
______________________________
Remove "_SPAM" to reply directly.
Apr 7 '06 #1
2 12306
You can use custom attributes like this.

<form>
<select name="selectname">
<option value="a,b,c" val1="a" val2="b" val3="c">first
selection</option>
</select>

<input
type="button"
value="Click Me"

onclick="dothis(this.form.selectname.options[this.form.selectname.selectedIndex].getAttribute(
'val1'))">

</form>

You still need the "value" tag since it contains the value that's
submitted.

Cheers,

-Georg
David Shorthouse wrote:
Folks,

I have a drop-down select menu generated server-side. I was hoping to
pass more than a single variable from the option value to a javascript
function. Is this possible? At the moment, my option values consist of three
numbers separated by commas. I thought I could pass these three numbers as
three distinct variables to the javascript function, but it obviously
doesn't work this way.

e.g.

<select name="selectname">
<option value="1,2,3">first selection</option>
etc.
</select>

<input type=button
onclick="dothis(document.form.selectname.options[document.form.selectname.selectedIndex].value)">

can't evidently be used as

<script lang="javascript">
function dothis(a,b,c) {
etc.
}
</script>

Any suggestions?

Dave
--
______________________________
Remove "_SPAM" to reply directly.


Apr 7 '06 #2
I also should say that the W3C DOM getAttribute() method allows you to
read any attribute from any tag. I haven't checked that on all current
browser versions though.

-Georg

David Shorthouse wrote:
Folks,

I have a drop-down select menu generated server-side. I was hoping to
pass more than a single variable from the option value to a javascript
function. Is this possible? At the moment, my option values consist of three
numbers separated by commas. I thought I could pass these three numbers as
three distinct variables to the javascript function, but it obviously
doesn't work this way.

e.g.

<select name="selectname">
<option value="1,2,3">first selection</option>
etc.
</select>

<input type=button
onclick="dothis(document.form.selectname.options[document.form.selectname.selectedIndex].value)">

can't evidently be used as

<script lang="javascript">
function dothis(a,b,c) {
etc.
}
</script>

Any suggestions?

Dave
--
______________________________
Remove "_SPAM" to reply directly.


Apr 7 '06 #3

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

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
1
by: carmen | last post by:
I am using the code below to open another page depending on what is selected. This is working ok but I would like to streamline it so that I will hot have to create a page everytime a new category...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
2
by: Dan | last post by:
Here is the working script if anyone is interested. I was missing the else if. <script language="JavaScript"> <!-- function Trip_Time(Traffic_Results) { if...
1
by: Dan Pavel | last post by:
Hi, I am new in programming and I have a problem. I must save a value in database but i cannot pass the value from javascript to VB at strSQL <% Sub setare(cs) If cs=1 Then strSQL="UPDATE...
1
by: msg2ajay | last post by:
hello , i have to pass a 'span' id to the javascript function'. how can i pass that. when i tried like below it is working!!! may the function is in the same page.... eg: ---- ...
2
by: kid | last post by:
Hi i need help i want to pass javascript variable to php. here's my sample code its not working. please someone help me.... <select name='dbPages' OnChange='getLayoutID(this.value)'> <option...
4
by: IRC | last post by:
hey, i am pretty new on javascript as well as PHP, Hey, anyone can you help me, how to pass the javascript array value to php page......... i want to retrieve the values which are arrayed on...
5
by: kodt | last post by:
I need to set an option from a select box to be selected based upon a value I am pulling from a database. I cannot alter the code of the select box in this case, so I must use javascript to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.