When an element is clicked for the first time, I would like its value to be set to empty.
Does the IF function work in jQuery? Is it like JavaScript?
I only want the data to be erased on the first time, of course, because it will be deleting the default value.
HTML
<input id="Name" class="inputLight" type="text" value="Enter your name here.">
jQuery
$('input').click(function() {
var $this = $(this),
开发者_开发技巧clickNum = $this.data('clickNum');
if (!clickNum) clickNum = 1;
if(clickNum =1)$this.value = "";
$this.data('clickNum', ++clickNum);
});
You don't need any if statements. You simply want to bind an event which will only be executed once.
Use jQuery's .one()
method:
$('input').one('focus', function(){
this.value = '';
});
精彩评论