MikeS
MikeS

Reputation: 383

Prevent textarea from clearing out when a new text is typed in

Here's my code for the text area.

<textarea style="width: 95%;" rows="6" name="feedback[<?=$r_uid;?>]" disabled="disabled" onclick="this.value='';" onblur="this.value=!this.value?'No feedback provided':this.value;" maxlength="800"></textarea>

When a user clicks it, it clears out. And if they leave it blank, the text "No feedback provided" appears. However, if they type something else and click outside the box, the text area retains it.

The problem, however, is if they click inside the box. The new text typed in is cleared out. Is there a way to prevent it from clearing out if a new text is typed in?

Thanks!

Upvotes: 0

Views: 524

Answers (2)

gdoron
gdoron

Reputation: 150283

Add this to your onClick event:

if (this.value == 'No feedback provided') this.value = '';

... Note that you only targeting click! what about keyboard navigation? use onfocus instead of onclick:

<textarea style="width: 95%;" rows="6" name="feedback[<?=$r_uid;?>]" disabled="disabled" 
onfocus="if (this.value == 'No feedback provided') this.value = '';" onblur="this.value=!this.value?'No feedback provided':this.value;" maxlength="800"></textarea>

Note that inline scripts are deprecated and hard to read and maintain.

Upvotes: 2

Rene Pot
Rene Pot

Reputation: 24815

This part in your code clears it. Remove it ;)

onclick="this.value='';"

Basically, when clicked on the element, the value of the clicked element = ''. So empty.

Upvotes: 0

Related Questions