views:

162

answers:

3

I want to copy data from one text box to another in html automatically ie., as I edit the first text box the second one should reflect the same spontaneously

A: 

In jQuery, something like this:

$("#txtBox1").keypress(function() {
  $("#txtBox2").val($(this).val());
}
womp
@womp: I saw no mention of jQuery in the question or the tags...
Andy E
@Andy E I used to wonder why people would get touchy about "use jQuery problem solved" responses, but it's starting to bug me too :-) (and I'm a huge jQuery fan)
Pointy
@Andy E's head - the code posted does exactly what he needs, and I saw no mention of "no jquery" either. jQuery makes handling events simple and automatically cross-browser.
womp
@Pointy, @womp: I think jQuery's brilliant, but I would hate to be a newcomer to a programming language, wanting to learn the ins and outs of said language and whenever I asked for help I was answered with a "use x framework" with no other explanation. It's strange, you don't really see it for other languages but you do with Javascript/jQuery.
Andy E
+3  A: 

call javascript function on onkeypresss

function copy_data(val){
 var a = document.getElementById(val.id).value
 document.getElementById("copy_to").value=a
}

EDITED USE onkeyup or onblur instead

<html>
<head>
    function copy_data(val){
     var a = document.getElementById(val.id).value
     document.getElementById("copy_to").value=a
    }

    </script>
</head>
<body>
<input type="text" name ="a" id="copy_from" onkeyup="copy_data(this)"/>
<input type="text" name ="a" id="copy_to"/>
</body>
</html>
Salil
Not enough jQuery ;-)
Andy E
A: 

You do easily with JQuery:

<input type="text" id="box1" />
<input type="text" id="box2" />

<script type="text/javascript">
$(function(){
  $("#box1").keypress(function()
  {
    $("#box2").val($(this).val());
  }
});
</script>
Sarfraz
You can easily do it without jQuery too. I don't understand the requirement to force-feed people with jQuery, sometimes jQuery isn't a necessity and sometimes people need to learn how to use the DOM properly first.
Andy E
@Andy E's head: Just to educate people how easy it is to do the things with jquery and solve cross browsers issues out of the box otherwise they will be using old style ways. We need to inform them to include jquery tag :)
Sarfraz