Mahesh KP
Mahesh KP

Reputation: 6446

Get text value of a watermark bound textbox from javascript

I want to get the text value of a textbox from javascript. The textbox has a watermark extender attached to it. So while taking the value from javascript, if the textbox is empty then also we are getting that water marktext.

Our markup is like

<asp:TextBox ID="txtname" runat="server" MaxLength="30"></asp:TextBox>
<asp:TextBoxWatermarkExtender ID="wmname" runat="server" TargetControlID="txtname"
                                WatermarkText="Name" WatermarkCssClass="txt">
                            </asp:TextBoxWatermarkExtender>

JS:

$('input').blur(function() {            
        alert($(this).val());
    });

On the blur event if we didn't enter any value , then also its alerting "Name".

Is there any way to get the exact text of textbox ie not the watermark text from javacript?

Upvotes: 0

Views: 2448

Answers (1)

coder
coder

Reputation: 13248

In the wrapper behavior, there is a property _isWatermarked that we can use to check whether the TextBox is watermarked.

Script:

<script>
function addValue() {
    if (!AjaxControlToolkit.TextBoxWrapper.get_Wrapper($get("TextBox1"))._isWatermarked) {
         $get("TextBox1").value = $get("TextBox1").value + ": Hello!";
     }
   }    
</script>

Controls:

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <cc1:TextBoxWatermarkExtender ID="TWE1" runat="server" Enabled="True" TargetControlID="TextBox1"
 WatermarkText="Type here:" WatermarkCssClass="watermarked" />

Upvotes: 2

Related Questions