Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update one input with parsed value from select list?

Status
Not open for further replies.

TheBlueDog

Technical User
Aug 23, 2002
13
US
I don't know any javascript, but I am wondering if anyone has any basic code they can show me to help me accomplish a task on a webpage I would like to create (all I really know is databases)...

I have a select list with a key value in the value field (which I must keep there to send to my database), and product description with price in the display. Since my users may occassionally have to change the price (this is for internal orders) I would like to parse out the price and display it in another input box on the form so it can be edited. I can use some common notation to distinguish between the product and the price (such as a dash followed by a space and a dollar sign), but since I don't know javascript I don't know how to either do the parse or put the price in a seperate field.

Can anyone give me a head start? I will gladly repay with any T-Sql or DBA help!! Many thanks... (I can give example if what I wrote doesn't make much sense)
 
TheBlueDog,

Hello, try this out I think by your description it is
a good example of what you are looking for. Let us know if
it needs an adjustment here or there.

Code:
<html><head><script>
function parse_sel(frm,opt){
eltxt = frm.sela.options[opt].text;
txt = eltxt.split(&quot;--&quot;);
frm.vala.value = txt[0];
frm.valb.value = txt[1];
}
</script></head><body>
<form name=&quot;test&quot;>
<input type=&quot;text&quot; name=&quot;vala&quot; size=&quot;15&quot;><br>
<input type=&quot;text&quot; name=&quot;valb&quot; size=&quot;15&quot;><br>
<select name=&quot;sela&quot; onChange=&quot;parse_sel(this.form, this.selectedIndex);&quot;>
  <option value=&quot;&quot;>Select One</option>
  <option value=&quot;guitar&quot;>Guitar--120</option>
  <option value=&quot;flute&quot;>Flute--95</option>
  <option value=&quot;sax&quot;>Sax--120</option>
  <option value=&quot;trumpet&quot;>Trumpet--65</option>
</select></body></html>[\code]

2b||!2b
 
Hey, thanks! That looks like it will efficiently accomplish the parse! Fantastic. How about using JS to put one of the parsed values into another input field? And could I also pass the other field name into the JS code so I could use the same function many times in a dynamically built page where the second input box name would change? For instance, I may have one or ten select lists on the page with the corresponding number of input boxes for each item's price. I am not sure how to put the parsed value in the field, and even more ideally I would like to have one function to also pass the field name to (though I could work it out without the last part, it just would be a lot more JS for the user to download...)

Thanks again,
Kim
 
TheBlueDog,

Glad it worked...

I've put a little something together here that will allow
you to generate your page, I'm guessing you are using
asp or php, and just index the last num of the form
elements accordingly. You can have as many sel lists on the
page as you need. Check it out if you need more explanation just shout back at us.

Code:
<html><head><script>
function parse_sel(frm,el,opt){
eltxt = el.options[opt].text;
txt = eltxt.split(&quot;--&quot;);
bind = el.name.indexOf(&quot;_&quot;) + 1;
txt_nm = el.name.substring(bind, el.name.length);
frm[txt_nm].value = txt[1];
}
</script></head><body>
<form name=&quot;test&quot;>
Price:<br>
<input type=&quot;text&quot; name=&quot;val1&quot; size=&quot;15&quot;><br>
<select name=&quot;sela_val1&quot; onChange=&quot;parse_sel(this.form, this, this.selectedIndex);&quot;>
  <option value=&quot;&quot;>Select One</option>
  <option value=&quot;guitar&quot;>Guitar--120</option>
  <option value=&quot;flute&quot;>Flute--95</option>
  <option value=&quot;sax&quot;>Sax--120</option>
  <option value=&quot;trumpet&quot;>Trumpet--65</option>
</select><br>
Price:<br>
<input type=&quot;text&quot; name=&quot;val2&quot; size=&quot;15&quot;><br>
<select name=&quot;sela_val2&quot; onChange=&quot;parse_sel(this.form, this, this.selectedIndex);&quot;>
  <option value=&quot;&quot;>Select One</option>
  <option value=&quot;guitar&quot;>Guitar--120</option>
  <option value=&quot;flute&quot;>Flute--95</option>
  <option value=&quot;sax&quot;>Sax--120</option>
  <option value=&quot;trumpet&quot;>Trumpet--65</option>
</select><br>
Price:<br>
<input type=&quot;text&quot; name=&quot;val3&quot; size=&quot;15&quot;><br>
<select name=&quot;sela_val3&quot; onChange=&quot;parse_sel(this.form, this, this.selectedIndex);&quot;>
  <option value=&quot;&quot;>Select One</option>
  <option value=&quot;guitar&quot;>Guitar--120</option>
  <option value=&quot;flute&quot;>Flute--95</option>
  <option value=&quot;sax&quot;>Sax--120</option>
  <option value=&quot;trumpet&quot;>Trumpet--65</option>
</select><br>
Price:<br>
<input type=&quot;text&quot; name=&quot;val4&quot; size=&quot;15&quot;><br>
<select name=&quot;sela_val4&quot; onChange=&quot;parse_sel(this.form, this, this.selectedIndex);&quot;>
  <option value=&quot;&quot;>Select One</option>
  <option value=&quot;guitar&quot;>Guitar--120</option>
  <option value=&quot;flute&quot;>Flute--95</option>
  <option value=&quot;sax&quot;>Sax--120</option>
  <option value=&quot;trumpet&quot;>Trumpet--65</option>
</select></body></html>

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top