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!

Select List(example)

Status
Not open for further replies.

FulFill

IS-IT--Management
Dec 5, 2001
13
US
Hi,

I need help with this JavaScript. What I’m trying to achieve is if user selects any item from list it should populate in text box if text box is already filled then it should not over ride the first one but it should populate in next available text box.

Now code is working only if the user selects list of items with CTRL and it will populate in all fields, but that’s not what I wanted they should have two options user can select one at a time or all at a time.

Conditions if user already selected item from the list and if by accidentally clicks on the same item it should give a warning mesg “not allowed” unless user delete’s and re-select
and the user have choice to remove any items and select diff one at any time.

Can you guys help with this I really appreciate for your help guys.


<html>

<head>

<script language=&quot;JavaScript&quot;><!--
function clicked(form) {
for (var i=0, j=0; i < form.selectName.options.length; i++) {
if (form.selectName.options.selected) {
form.elements['currentText' + j].value = form.selectName.options.text;
form.elements['currentValue' + j].value = form.selectName.options.value;
j++;
}
}
}
//--></script>

</head>

<body>

<form name=&quot;formName1&quot; onSubmit=&quot;return false;&quot;>
<select name=&quot;selectName&quot; multiple>
<option value=&quot;option 0&quot;>entry 0
<option value=&quot;option 1&quot;>entry 1
<option value=&quot;option 2&quot;>entry 2
<option value=&quot;option 3&quot;>entry 3
<option value=&quot;option 4&quot;>entry 4
<option value=&quot;option 5&quot;>entry 5
</select>
<input type=&quot;submit&quot; value=&quot;Enter&quot; onClick=&quot;clicked(this.form);return false;&quot;>
<input type=&quot;reset&quot; value=&quot;clear&quot;>

<p>
Select items from the above menu, then press enter.
The following boxes will display your choices:
</p>

<p>
<input name=&quot;currentText0&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue0&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText1&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue1&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText2&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue2&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText3&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue3&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText4&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue4&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText5&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue5&quot; type=&quot;text&quot; value=&quot;&quot;>
</form>

</body>

</html>

Thank you
 
FulFill,

Try this out:

Code:
<html><head><script language=&quot;JavaScript&quot;><!--
function clicked(form) {
var fnd = 0;
var added = 0;
for (var i=0;i < form.selectName.options.length; i++) {
  if (form.selectName.options[i].selected == true) {
	var found = false;
   rex = new RegExp(form.selectName.options[i].value);
     for(var k=0;k<form.elements.length;k++) {
	if(form.elements[k].type=='text' && form.elements[k].value.match(rex)){
	found = true; fnd++; break;}}
	if(!found){ for(var j=0;j<form.elements.length;j++) {
	if(form.elements[j].type == 'text' && form.elements[j].value == &quot;&quot;) {
	form.elements[j].value = form.selectName.options[i].text;
	form.elements[j+1].value = form.selectName.options[i].value;
	added++; break;}}}
}}
if(fnd>0){ alert(fnd +' Value(s) Already Existed, '+ added +' Field(s) Were Updated.');}}
//--></script></head><body>
<form name=&quot;formName1&quot; onSubmit=&quot;return false;&quot;>
<select name=&quot;selectName&quot; multiple>
<option value=&quot;option 0&quot;>entry 0</option>
<option value=&quot;option 1&quot;>entry 1</option>
<option value=&quot;option 2&quot;>entry 2</option>
<option value=&quot;option 3&quot;>entry 3</option>
<option value=&quot;option 4&quot;>entry 4</option>
<option value=&quot;option 5&quot;>entry 5</option>
</select>
<input type=&quot;button&quot; value=&quot;Enter&quot; onClick=&quot;clicked(this.form);return false;&quot;>
<input type=&quot;reset&quot; value=&quot;clear&quot;>

<p>
Select items from the above menu, then press enter.
The following boxes will display your choices:
</p>

<p>
<input name=&quot;currentText0&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue0&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText1&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue1&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText2&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue2&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText3&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue3&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText4&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue4&quot; type=&quot;text&quot; value=&quot;&quot;>
<br>
<input name=&quot;currentText5&quot; type=&quot;text&quot; value=&quot;&quot;>
<input name=&quot;currentValue5&quot; type=&quot;text&quot; value=&quot;&quot;>
</form></body></html>

Let us know if you have any questions.

2b||!2b
 
Thank you, Lrnmore this is exactly what I needed you ARE THE BESSSSSSSSST [medal].

Thank you again! Saved my life 2B :)
 
One more issue when I give input text names it gives me error mesgs ...I Highlighted in bold.

if(form.elements['currentText' + j].type == 'text' && form.elements['currentText' + j].value == &quot;&quot;) {
form.elements['currentText' + j].value = form.selectName.options.text;
form.elements['currentValue' + j+1].value = form.selectName.options.value;

because I have more text boxs and I want to give the name to where to load

Thank you.
 
Try ref. the elements differently add the red code:

function clicked(form) {
var el = document.formName1;
var fnd = 0;

and this section:
Code:
if(!found){ for(var j=0;j<form.elements.length;j++) {
    if(form.elements[j].type == 'text' && form.elements[j].value == &quot;&quot;) {
    form.elements[j].value = form.selectName.options[i].text;
    form.elements[j+1].value = form.selectName.options[i].value;
    added++; break;}}}

---to---

if(!found){ for(var j=0;j<6;j++) {
  if(el['currentText'+j].value == &quot;&quot;) {
  el['currentText'+j].value = form.selectName.options[i].text;
  el['currentValue'+j].value = form.selectName.options[i].value;
    added++; break;}}}

Let me know if works out for ya...




2b||!2b
 
Fulfill said &quot;Thank you, Lrnmore this is exactly what I needed you ARE THE BESSSSSSSSST &quot;

-- Sounds like lrnmore deserves a star??? Click on &quot;Mark this post as a helpful/expert post!&quot; to give one if you think so.

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top