I am trying out this simple code. What I want is that when a paste event is fired on the second input textbox, it should be cleared, after copying its contents, removing the readonly attribute of the previous textbox, and pasting it there. however, nothing is happening.
The paste event is fired alright, because if I replace the code in the timer by a simple alert, it works. Can anyone tell me what’s wrong here?
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".boo").bind("input paste",function() {
elem = this;
setTimeout(function() {
$(".foo").removeAttr("readonly");
$(".foo").text($(elem).text());
$(elem).text("");
},100);
});
});
</script>
</head>
<body>
<input class = 'foo' type = 'text' /><input class = 'boo' type = 'text' />
</body>
</html>
First of all, you should use
.val()instead of.text()with input control.Also, your bound event(s) were fired twice when text is pasted in the control. That’s because, you have bound both
inputandpasteevents to the element(s) with “boo” class.So here, instead of:
Use this:
This will bind only the
pasteevent to input elements with “boo” class.See updated jsFiddle example.