Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery keyup value of input

i need to display value of <input> into the <span> and when label become empty, change value of span to "Video title". But always i type something into the <input> and then delete it, the <input> still have "" value so <span> display nothing. Thanks for help. (And sorry for my bad english)

Here is my code:

$("#newVideoName").keyup(function(){
   var newVideoName = $("#newVideoName").val();
   if(newVideoName == ""){
       $("#newVideoNameLabel").html("Video title");
   }
});
<input type="text" id="newVideoName">
<span id="newVideoNameLabel"></span>
like image 899
Gabriel Uhlíř Avatar asked Jun 17 '12 12:06

Gabriel Uhlíř


People also ask

What does Keyup return?

The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .

What is Keyup and Keydown in jQuery?

keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.

Why is keypress deprecated?

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes.

Why is Keyup not working?

keyup / keydown seem to only work on elements that are present at document. ready . If you are triggering your event from elements that were altered or injected post pageload, these events will not fire.


1 Answers

$("#newVideoName").keyup(function() {
   $("#newVideoNameLabel").text(this.value ? this.value : "Video title");
});

http://jsfiddle.net/gZPyQ/

like image 68
undefined Avatar answered Oct 05 '22 14:10

undefined