Clear text with JavaScript in MVC 4

I've the following search box:

@Html.TextBox("SearchString", "search...", new { @class = "SearchTxtBox" })

A text box with a default text "search...", I want to clear the text when the user enters the text box like we do in pure html:

<input type="text" value="search..." onfocus="if(this.value=='search...') this.value='';">

Any ideas how to do so in MVC 4?

Answers


If you want to do the traditional way you cna do it this way:-

@Html.TextBox("SearchString", "search...", new { @class = "SearchTxtBox",onfocus="if(this.value=='search...') this.value='';" });

You can utilize HTML5 placeholder attribute too...

@Html.TextBox("SearchString","", new { @class = "SearchTxtBox", placeholder = "search..." });

Be aware of the fact that it is not supported in older browsers

If you want to know more about its support and workaround you can go through the below thread.

HTML5 "placeholder" support


Need Your Help

C - How to create a pattern in code segment to recognize it in memory dump?

c assembly embedded

I dump my RAM (a piece of it - code segment only) in order to find where is which C function being placed. I have no map file and I don't know what boot/init routines exactly do.

show paragraph on image mouseover

jquery hover onmouseover

I have images in divs and I would like to have paragraphs to appear on top of them on mouseover, using jQuery. The paragraphs are initially hidden.