简体   繁体   中英

How to clear the text when clicked on any other controls

the below js script works fine but there is small problem and the problem is that:

i have a textbox with multiline and when the user clicks on it and it does clear the textbox but the problem is; when the user click on Save button (asp.net control button) to save the textbox value and i want that to ignore on code behind and also client side.

one thing i can do is to compare the value and clear if it is matached but what if; if i have many controls and dont want to spread the string every where in the code...

looking for a optimized solution.

<asp:TextBox runat="server" ID="txtNew" 
onclick="if (this.value == 'Enter here...') this.value = ''" TextMode="MultiLine"
Rows="7" Width="100%">Enter here...</asp:TextBox>

Sort of looks like you are trying to do a watermark feature on the textarea/textbox's?

Why not check out the html5 placeholder attribute? Check out this example here http://jsfiddle.net/k2PLG/1/

The placeholder attibute is not a value that is posted or validated against. Also, since you are using javascript, then you can make sure that you include an html5 javascript file which makes this attibute work in older browsers which do not support it.

You don't need to hardcode your Enter here... , use defaultValue instead.

<asp:TextBox runat="server" ID="txtNew" 
onclick="if (this.value == this.defaultValue) this.value = ''" TextMode="MultiLine"
Rows="7" Width="100%">Enter here...</asp:TextBox>

For the codebehind part of your question:

I would recommend to use a RequiredFieldValidator (since "Enter here" suggests that it's mandatory). Then you can use the InititalValue property for your default text, hence the user must enter something to submit the page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM