Search code examples
grailstextfield

Grails: How do I make a g:textfield to load some data and display it in other g:textfield?


I have two g:textfields in the first one I should write a number lets say 12 and in the g:textfield next to it it should load the predetermined name for number 12.

The first one is named 'shipper' and the other 'shipperName' Whenever I write the code 12 in the 'shipper' txtfield, it should return the name of the shipper in the 'shipperName' box.

Thanks in advance!

Examples:

If I write the number 12 it should return USPS

http://i53.tinypic.com/2i90mc.jpg

And every number should have a different 'shipperName'

Thanks again!


Solution

  • 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/

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