Reputation: 1472
I want to print the value of a input field real-time to a DIV.
Right now I have got this:
$("#page_name").change(function () {
var new_val = $("#page_name").val();
$("#page_url").html(new_val);
});
It doesn't work, obviously. Anyone got a suggestion?
Thanks!
Upvotes: 1
Views: 1651
Reputation: 131
As pointed out 'change' only occurs when the element loses focus. An alternative to binding it to the keypress event would be binding it to the "input" event.
$('#page_name').bind('input', function(e){
var output = e.target.value;
$('#page_url').text(output);
});
The problem with keypress is that it does not work on copy&paste text.
The input event is pretty much what everybody expects 'change' to be (o: I think this event is pretty new (HTML5?), older browsers may not get that one.
Upvotes: 3
Reputation: 13723
Actually it will work, but only when you move the cursor out of the input box, because change fires when the focus is lost.
What you need is keypress. The code is just the same as yours
$("#page_name").keypress(function () {
var new_val = $("#page_name").val();
$("#page_url").html(new_val);
});
test it here
Upvotes: 0
Reputation: 3234
That should actually work. My best guess is this:
#page_name
that second time.Upvotes: 3