javascript - Give textfield a value based on option selected from drop down -


what i'm trying give textfield value based an option select form drop down. example: have 2 fields, drop down , textfield. select facebook dropdown , value "http://www.facebook.com/" appears in textfield. how can achieve effect? know have call function onchange of drop down that's pretty know. remember i'm not trying copy exact selected value dropdown textfield here.

example markup

<select>     <option value="http://www.facebook.com">facebook</option>     <option value="http://www.twitter.com">twitter</option> </select>  <input type="text" /> 

jquery

$('select').change(function() {     $('input[type="text"]').val(this.value); }); 

here's fiddle


in response comment, there number of ways (a switch statement, if/elseif statement etc), easiest create object mapping text corresponding url:

var urlfromtext = {   'facebook' : 'http://www.facebook.com/',   'twitter' : 'http://www.twitter.com/' }; 

then, in change handler, can use:

$('input[type="text"]').val(urlfromtext[$('option:selected', this).text()]); 

here's example


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -