Jump to content

onchange fill input box


mraza

Recommended Posts

hi i need a little help with javascript, I have one drop downl list which is populating from mysql

 

<script type="text/javascript">
function show() {
// dont know what to put here  :-\
}
<script>

<select name="user" onchange="show()">
<option value="0">Select a user </option>
<option value="1">First Name of one user </option>
<option value="2">First Name of second user</option>
<option value="3">First Name of third user</option>
</select>

<input type="text name="fname" value="" />

 

Now what i wants is when i change first option to second it will fill value="" of input box with that user second name which i need to run mysql query but i am not good with javascript so really appreciate for any help. Thanks

Link to comment
Share on other sites

Try something like this, not tested:

 

<script type="text/javascript">
function updateUser(obj) {
   var selected = obj.options[obj.options.selectedIndex].value;
   document.getElementById('fname').value = selected;
   if(selected == 0){
      document.getElementById('fname').value = '';
   }
}
</script>

<select name="user" onchange="javascript: updateUser(this)">
<option value="0">Select a user </option>
<option value="firstname1">First Name of one user </option>
<option value="firstname2">First Name of second user</option>
<option value="firstname3">First Name of third user</option>
</select>

<input type="text" id="fname" name="fname" value="" />

 

What I did was change the dropdown box so that the value of each option is also the username, so that you can grab the value. Didn't test it, just going from memory really. I tend to use JavaScript frameworks these days anyway.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.