asp.net - JavaScript: Clear search-field when enter with mouse? - Stack Overflow

I have a textfield () in my homepage for a searchstring.Normaly I have a text like "enter here to

I have a textfield () in my homepage for a searchstring. Normaly I have a text like "enter here to searach..." in it.

Now I will clear the box from the text when a user click into it.

How to solve? JavaScript?

I have a textfield () in my homepage for a searchstring. Normaly I have a text like "enter here to searach..." in it.

Now I will clear the box from the text when a user click into it.

How to solve? JavaScript?

Share Improve this question edited Mar 7, 2011 at 12:17 Mathias Bynens 150k54 gold badges222 silver badges251 bronze badges asked Mar 7, 2011 at 12:09 PassionateDeveloperPassionateDeveloper 15.2k35 gold badges111 silver badges190 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

This can be done by using the placeholder attribute in HTML. Example:

<input type="text" placeholder="e.g. John Doe">

For browsers who don’t support this attribute natively, you could use JavaScript.

If you’re using jQuery, you could consider using my placeholder plugin for jQuery, which is the most robust solution I’ve seen. Here’s a demo page: http://mathiasbynens.be/demo/placeholder

create a JavaScript file or use a current one then write in it:

Function function_name()
{

    document.getEelementByName("Control_Name").setAttribute("Value", '');
}

then in the page head:

<script id="js1" type="text/javascript" src="dir/file_name.js"></script>

The Control Tag bee:

<input type="text" name="Control_Name" onClick="return js1/function_name();" />

Yes you need javascript to achieve this. You could subscribe for the onclick event and when this event is triggered set the text to empty and then unsubscribe from the onclick event to avoid clearing the text every time a user clicks:

var foo = document.getElementById('foo');
foo.onfocus = function() {
    foo.value = '';
    foo.onfocus = null;
};

Live demo.

And if you are using jquery there is a really nice watermark plugin which allows you to do this.

I would suggest writing an onfocus Javascript handler; that will be triggered whether you’ve clicked it or tabbed into it.

<input type="text" name="..." value="" onfocus="this.value = '';" />

You can use onfocus and onblur javascript methods

<input type="text" name="searchinput" onblur="this.value='enter here to search...';" onfocus="this.value = '';" />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745642066a4637752.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信