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
2010-05-27 16:39:16
@womp: I saw no mention of jQuery in the question or the tags...
Andy E
2010-05-27 16:52:32
@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
2010-05-27 16:55:10
@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
2010-05-27 17:12:21
@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
2010-05-27 18:36:41
+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
2010-05-27 16:41:42
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
2010-05-27 16:45:59
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
2010-05-27 16:54:10
@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
2010-05-27 17:00:21