-1

It's a very basic question which bugging me and nothing seems working. The page contain 2 forms (source-target) and I want to pass the value of one input(#query2) to another (#query) on blur event.

    $(document).ready(function() {
    $("#query2").blur(function(e){
        //test 1
        $('form[name=searchheadForm] #query').val($(this).val());
        //test 2
        $("form#search_head#query").val($(this).val());
    });
    });

Any idea? TIA.

Martijn
  • 15,791
  • 4
  • 36
  • 68
Jonathan.B
  • 91
  • 4
  • 14

3 Answers3

1

Try this example :

jquery

  <script type="text/javascript">
     $(document).ready(function(){
        $('#query1').change(function(){
            var val = $('#query1').val();
            $('#query2').val(val);
        });
     });
</script>

HTML

<input type="text" id="query1"/>
<input type="text" id="query2"/>
Devang Rathod
  • 6,650
  • 2
  • 23
  • 32
1
$(document).ready(function() {
   $("#query2").blur(function(){
       $('#query').val($(this).val());
   });
});

That should take the value from #query2 and put it into #query on blur. You also use keypress to do it in realtime.

Pat Dobson
  • 3,249
  • 2
  • 19
  • 32
  • This code work on a fly but also I need to reference through form name or id, whatever is working. Thanks anyway, found the solution. – Jonathan.B Sep 05 '13 at 08:58
0

Both of your selectors are wrong, here are the better solutions:

// Your method 1, I've added quotes. Not 100% needed, but it's good practice
// Also, I've encountered cases where no quotes gave an error:
$('form[name="searchheadForm"] #query').val($(this).val());

// Your method 2 
$("form#search_head #query").val( $(this).val() ); // space for #query

// or the last one better (this method is the one I recommand, id is always faster)
$("#query").val( $(this).val() ); // There is always 1 ID, so a ID selector only needs 1 refference

If you did this, it will work:

http://jsfiddle.net/VbVdM/2/ <- working example

Martijn
  • 15,791
  • 4
  • 36
  • 68
  • 1) Your fiddle proves the quotes aren't needed. 2) I explained my downvote 3) don't repost your answer to try to dodge downvotes – Denys Séguret Sep 05 '13 at 08:26
  • The 2nd and 3rd option works on a fly. thanks. But for some reason option did not work. Both form name and input id are correct. – Jonathan.B Sep 05 '13 at 08:55