input field and drop down select option

Javascript coding ..

Moderators: macek, egami, gesf

dd09
New php-forum User
New php-forum User
Posts: 34
Joined: Wed Jan 11, 2012 10:01 pm

input field and drop down select option

Postby dd09 » Mon Jun 11, 2012 10:17 pm

i want the vice-versa of what the following code is giving output.... i.e. if the user types in the input
field then the drop down list related to that value should get updated...

e.g
if i type Mr. in input field the from drop down Mr. should get selected...
if typed Dr then from option Dr. should get selected..


Code: Select all

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(function() {
    // initialize the value of the input field to the selected value
    // of the dropdown list:
    $('#gift_title').val($('#title').val());

    $('#title').change(function() {
        // when the value of the dropdown list changes
        // update the input field
        $('#gift_title').val($(this).val());
    });
});
</script>
<html>
</body>

<select name="title" id="title">
  <option <a href="abstract.jpg">Mrs</a></option>
  <option value="Mr" >Mr</option>
  <option value="Miss" >Miss</option>
  <option value="Dr" >Dr</option>
 
  <input type="text" name="gtitle" id="gift_title" a href="" />
</select>
</body>
</html>

User avatar
ejsexton82
New php-forum User
New php-forum User
Posts: 86
Joined: Mon Jun 04, 2012 10:05 pm
Location: Ankara, Turkey
Contact:

Re: input field and drop down select option

Postby ejsexton82 » Tue Jun 12, 2012 1:06 am

Try something like this:

Code: Select all

$('#gift_title').bind('change keyup',function(){
   var value = $(this).val();
   if(value.search(/^Mrs\b/i) == 0) {
      $('#title').val('Mrs');
   }
   else if(value.search(/^Mr\b/i) == 0) {
      $('#title').val('Mr');
   }
   else if(value.search(/^Dr\b/i) == 0) {
      $('#title').val('Dr');
   }
   else if(value.search(/^Miss\b/i) == 0) {
      $('#title').val('Miss');
   }
});


Note: This script may have problems with names that start with "DR" like "DRaco Malfoy".


Return to “JavaScript”

Who is online

Users browsing this forum: No registered users and 1 guest