1

I'm using ASP.NETand I want to show message behind a textbox when I start to write in it, I want to do that on the client side so I think javascript is the key.

Edit :

My code:

<tr>
                    <td>
                        <asp:Label ID="Label1" runat="server" Text="Alternate Email :"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="_txtAlternateEmail" runat="server" onkeyup="typetext(this);"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:Label ID="Label2" runat="server" Text="Security Question :"></asp:Label>
                    </td>
                    <td>
                        <asp:TextBox ID="_txtSecurityQuestion" runat="server"></asp:TextBox>
                    </td>
                </tr>

and the javascript

<script type="text/javascript">
    function typetext(tb) {
        var mySpanElement = document.getElementById("_txtSecurityQuestion");
        mySpanElement.innerText = tb.value;
    }
</script>
2
  • Do you mean you want to show the text next to the textbox, as the user's typing it? Commented Jan 5, 2012 at 0:37
  • exactly and this text disappear if textbox is empty Commented Jan 5, 2012 at 0:40

1 Answer 1

1

You could handle the onkeyup event in the textbox

<asp:TextBox ID="_txtAlternateEmail" runat="server" onkeyup="typetext(this);">
</asp:TextBox>

which calls the typetext method. In your typetext method, you can set the span (or whatever HTML element) to the text of the textbox.

function typetext(tb)
{
     var mytextbox = document.getElementById('<%=_txtSecurityQuestion.ClientID %>');
     mytextbox.value = tb.value;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried but nothing changed, meanwhile I'm using the asp.net textbox not the normal html input text type
Please post your code. The above works with asp.net textboxes as well.
I've updated my answer so that it writes to a textbox instead of a span. To get asp.net controls in Javascript, you need to use <%= %>. The above should now work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.