Old 16-05-2007, 15:12   #1 (permalink)
Uru Wolf
Website Designer
 
Join Date: May 2007
Posts: 7
[JavaScript] Ugh...

I have a dropdown box that loads the name and ID of images stored in a MySQL (viai PHP) into a dropdown box. I need to have a script so when a button is pressed a string + the image id (var basically) + another string are inserted into a text box.

Eg:
Dropdown box:
<option name="ID">an image</option>

now when a button is clicked -> add 'str' + ID + 'str' to a textbox.

Any help anyone?
  Reply With Quote
Old 16-05-2007, 18:48   #2 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 200
Send a message via AIM to Cborrow
Well you can get the name and value of the selected dropdown like so, I believe.

Code:
var selectedItem = document.forms[0].selectName.options['document.forms[0].selectName.options.selectedIndex]; var name = selectedItem.getAttribute('name'); var value = selectedItem.value;

then to add it to a textbox

Code:
document.forms[0].textBoxName.value = str;

I think that might work.

You can replace forms[0] with your form name and selectName and textBoxName are the names of the select and textbox

e.g.
Code:
<input type="text" name="textBoxName" /> <select name="selectName"> </select>
  Reply With Quote
Old 17-05-2007, 13:59   #3 (permalink)
Uru Wolf
Website Designer
 
Join Date: May 2007
Posts: 7
Thanks so much ^_^
  Reply With Quote
Old 18-05-2007, 11:14   #4 (permalink)
Uru Wolf
Website Designer
 
Join Date: May 2007
Posts: 7
No, this is not doing it >_< I click the button and nothing happens. I think it would be better if it where in a function to. Man I hate javascript.
  Reply With Quote
Old 18-05-2007, 13:19   #5 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 200
Send a message via AIM to Cborrow
Ok here is a working example with functions and all.

HTML Code:
<html> <head> <title>Test</title> <script type="text/javascript"> function GetSelectedValue(select) { var id = select.options[select.selectedIndex].getAttribute('name'); AddToInput(document.forms[0].textBox1, 'string_left ' + id + ' string_right'); } function AddToInput(input, value) { input.value = value; } </script> </head> <body> <form action="" method="get"> <p> <label>Select image</label> <select name="TestSelect"> <option name="1">Image 01</option> <option name="2">Image 02</option> <option name="3">Image 03</option> </select><br /><br /> <label>Selected value</label> <input type="text" name="textBox1" /><br /><br /> <input type="button" name="getValue" value="Get value" onClick="GetSelectedValue(document.forms[0].TestSelect)"; /> </p> </form> </body> </html>
  Reply With Quote
Old 19-05-2007, 06:14   #6 (permalink)
Uru Wolf
Website Designer
 
Join Date: May 2007
Posts: 7
=\ I hate to be a pain but is is still not working. Could it be the order that my script is loaded? (php -> javascript -> html output)
  Reply With Quote
Old 19-05-2007, 08:14   #7 (permalink)
Cborrow
I like code.
 
Join Date: Dec 2004
Location: Chesapeake, VA
Posts: 200
Send a message via AIM to Cborrow
It could be a possiblilty. But does this example work?

Test
  Reply With Quote
Old 20-05-2007, 11:04   #8 (permalink)
Uru Wolf
Website Designer
 
Join Date: May 2007
Posts: 7
Yes your example works fine.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8