views:

79

answers:

4

I have two text fields and whenever I write a number in the first one, it should give me its name on the second field.

Let's say I write 11 on the "first" field, then it should automatically put the word 'eleven' (NOT number itself) on the "second" field. And the same thing with any number.

I know this can be done with jQuery but I just don't know how. Any suggestions??

+3  A: 
$(function() {
  $('.input').bind('keyup',function() {
     $('.output').val($(this).val());
  });
});

you can Test it here http://jsbin.com/owani3

Ninja Dude
totally the same answer as mine 2 seconds interval :)
Nik
using `.live()` is bit complex though, for simple jobs `.bind()` is just fine :)
Ninja Dude
But I need to load some text, not the same thing I write.
fgualda87
check comment on answer above
fgualda87
Ninja Dude
I just wanna make the second field show some text depending on the number in the first field
fgualda87
+1  A: 

This should do the job

$('#f1').live('keyup', function(){
    $('#f2').val($(this).val())
})

Check working demo: http://jsfiddle.net/E44Un/

Nik
+1  A: 

For any number of textboxes, this should work:

​​$(".syn").live("keyup", function() {
  var self = $(this);
  $(".syn").each(function() {
    $(this).val(self.val());
  });
})​​​​​​​​​​​​​​​

just put a class syn to your text input

PeterWong
You should declare "self" using `var` to prevent it being a global variable.
Matt
Ooops, thanks ^^
PeterWong
+1  A: 

To relate two strings, it's easiest to use an object to create a dictionary/ map, as shown below;

$('#input1').bind('keyup',function() {
     var map = {
         "1":"One",
         "2":"Fish",
         "3":"Bar"
     };

     $('#input2').val(map[$(this).val()]);
});

You can see this in action here: http://www.jsfiddle.net/dCy6f/

For more advanced behaviour:

$('#input1').bind('keyup',function() {
     var str = '';
     var input = $(this).val();
     var intVal = parseInt(input, 10); // Dont forget the radix
     var map = {
         "1":"One",
         "2":"Fish",
         "3":"Bar"
     };

     if (intVal > 50 && intVal < 100) {
         str = 'Something';
     } else if (map.hasOwnProperty(input)) {
         str = map[input];
     }

     $('#input2').val(str);
});

You can test this inplementation here: http://www.jsfiddle.net/H6skz/

If you want the second value only to update when the user has finished typing into the first input field, change "keyup" to "change".

To abstract this into a function, your could do:

function relate(me, withMe) {
    $(me).bind('keyup',function() {
         var str = '';
         var input = $(this).val();
         var intVal = parseInt(input, 10); // Dont forget the radix
         var map = {
             "1":"One",
             "2":"Fish",
             "3":"Bar"
         };

         if (intVal > 50 && intVal < 100) {
             str = 'Something';
         } else if (map.hasOwnProperty(input)) {
             str = map[input];
         }

         $(withMe).val(str);
    });
}

and then use it as follows:

relate('#input1', '#input2');

For a more intuitive interface, write a jQuery plugin:

(function ($) {
    jQuery.fn.relate = function (withMe) {
        this.bind('keyup',function() {
             var str = '';
             var input = $(this).val();
             var intVal = parseInt(input, 10); // Dont forget the radix
             var map = {
                 "1":"One",
                 "2":"Fish",
                 "3":"Bar"
             };

             if (intVal > 50 && intVal < 100) {
                 str = 'Something';
             } else if (map.hasOwnProperty(input)) {
                 str = map[input];
             }

             $(withMe).val(str);
        });
    };
}(jQuery));

and then use as follows:

$('#input1').relate('#input2');
Matt
Excellent!! Thank you very much!!
fgualda87
This is exactly what I needed!!
fgualda87
How would it need to be if I wanted the rest of the numbers to be just blank??
fgualda87
Or like a range of numbers from 11-99
fgualda87
@fgualda87: See my edit
Matt
perfect! Thank you sooo much!! If I wanted to use it with a drop down box, do you think it'll work too?? Like what if I want that for every option on the first box, to display certain options on the second box??
fgualda87
Here's the link to the other question. Just in case.
fgualda87
http://stackoverflow.com/questions/3852129/jquery-load-data-on-a-combo-box-depending-on-another
fgualda87
Hey I have a question, let's say I have to use those same numbers and names in different textfields, is there any way to make it like a javascript function with parameters??? Like function codes(input1, input2){ .... all the jQuery... } ??? Thanks
fgualda87
@fgualda87: See my edit.
Matt
Perfect!! Thanks again!How do I make the value of the textfield be the text of the field?? Because when I submit the form, those textfields are blank :(
fgualda87
@fgualda87: I don't know what you mean?
Matt
Nvm, I fixed it, I just forgot to link the input2 textfields with the rest of the form, thank again Matt!!
fgualda87