0

I have two different arrays - Name and Email in javascript. Values in the two arrays correspond to each other; Name[0] has email value at Email[0]. Values from array Name are used for a dropdown in the HTML page. Now when the user selects a name (dropdown) and clicks on Send button, email should be send to the person whose name is selected in the dropdown. How to lookup in the second array based on the value selected by User in the dropdown? Name and Email arrays will always have dynamic values

Thanks.

1
  • Can't you use object for the 2nd array? var emails = {}; emails["userName1"] = email1; emails["username2"] = email2; then when the drop down changes sendEmail(emails[dropDownValue]); Commented Sep 20, 2014 at 6:48

2 Answers 2

1
var email = Email[document.getElementById("names").selectedIndex]
Sign up to request clarification or add additional context in comments.

Comments

0

Value of selected index is to provide the required value, so you can initialize it:

function getValue(){      
    var emailSelectedUser = document.getElementById("names").value;
    alert(emailSelectedUser)
 }
 <select id='names'>
  <option value="[email protected]">User Name</option>
   <option value="[email protected]">User 2</option>
 </select> 
<input type='button' value='submit' onclick='getValue()' />

Comments

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.