开发者

First time a text Input is selected, delete its value

开发者 https://www.devze.com 2023-04-05 23:09 出处:网络
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?

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 = '';
});
0

精彩评论

暂无评论...
验证码 换一张
取 消