0

I need to populate 2 different text boxes when something is selected in the dropdown box.

From the fiddle, i can be do it with one.

Sorry, let me clarify a little more. This dropdown list actually is the person's LONG NAME. I'm trying to populate a "nickname" and "contact number" textbox when his name is selected.

Look at this demo which I now have 2 textbox in there. In my database, I have a record of everyone's Fullname, nickname, and contact number. It's up to me how I create a select list, but I only want to select his full name to show the nickname and contact number on 2 seperate boxes..

4
  • ?? Why not just add code to that event handler to update another input field? What's the problem? Commented Jan 30, 2013 at 15:59
  • What do you mean by that? Commented Jan 30, 2013 at 16:10
  • I mean you've got code right there to update a textarea already. What is it that's not working about simply using that approach on another input field? Commented Jan 30, 2013 at 16:12
  • because textfield value = selected value I don't know where to populate with another value. is Array allwed? Commented Jan 30, 2013 at 16:21

4 Answers 4

1

Sorry but your example has only one texarea...

However if I don't' have misunderstood your question you can simply do that:

var mytextbox = document.getElementById('mytext');
var mytextbox2 = document.getElementById('mytext2');

var mydropdown = document.getElementById('dropdown');

mydropdown.onchange = function(){

   mytextbox.value = mytextbox.value  + this.value;
   mytextbox2.value = mytextbox2.value  + this.value;
}
Sign up to request clarification or add additional context in comments.

Comments

1
<textarea id="mytext"></textarea>
<textarea id="mytext2"></textarea>
<select id="dropdown">
<option value="">None</option>
<option value="contactnumber1">text1</option>
<option value="contactnumber2">text2</option>
<option value="contactnumber3">text3</option>
<option value="contactnumber4">text4</option>
</select>

var mytextbox = document.getElementById('mytext');
var mytextbox2 = document.getElementById('mytext2');
var mydropdown = document.getElementById('dropdown');

mydropdown.onchange = function () {
mytextbox.value = mydropdown.options[this.selectedIndex].text;
mytextbox2.value = mydropdown.options[this.selectedIndex].value;
};

Comments

0

do you mean, you want to append selection each time some body selects item from drop down? I do not see two text boxes in your fiddle that is why I am asking...

if you want unique values selected then..

var mytextbox = document.getElementById('mytext');
var mydropdown = document.getElementById('dropdown');
var strValue =  new String();
mydropdown.onchange = function(){
if(strValue.indexOf(this.value)<=0){
   if(strValue!="")
{
    strValue += "," + this.value;
}
else
{
    strValue = this.value
}
}
mytextbox.value = strValue;
}

2 Comments

Sorry, let me clarify a little more. This dropdown list actually is the person's LONG NAME. I'm trying to populate a "nickname" and "contact number" textbox when his name is selected.
where are the contact number's coming from (for each long name)?
0

So you are wanting two values to be populated by one tag. With that said, what if you just put data attributes in the option tag? data-contactNumber data-longName

<select>  
<option value="x" data-contactNumber = "yyyy" data-longName="zzzz">  
.....
</select>  

Then you can just assign the selected option text boxes the individual data attrs??

4 Comments

I can actually do this?! I'm going to try!
wait, how do I get the data from it?
Data ATTRS are a wonderful thing and should do exactly what you want. Glad you found the other link as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.