开发者

on click for asp:textbox?

Im looking for a way to have a textbox contain a bit of text until a users clicks into it to enter their own text. Its a search field and the customer wants the textbox to have "Sea开发者_JS百科rch Stock" in the textbox, then when a users clicks in it, it becomes null so they can enter their own criteria. I cant see an onclick or similar in the designview for the textbox though, any hints?

thanks


<asp:TextBox runat="server" value="Search Stock" ID="textbox1"
             onfocus="if (this.value == 'Search Stock')  this.value = ''; "
             onblur="if (this.value == '')  this.value ='Search Stock'; " />


It's called "placeholder text" - search for that, there are lots of examples (including built-in in html5)


Pre populate the Text property of the TextBox with the value "Search Stock". Then use javascript, and on the OnClick client event of the TextBox...clear the Text.


Here's a quick and dirty version.

<asp:TextBox ID="overlayTextbox" runat="server"ForeColor="#A0ABC6" Text="Search Stock" onfocus="hidePlaceholder(this);"></asp:TextBox>

and the script to hide the placehoder:

    <script type="text/javascript">
    function hidePlaceholder(sender) {
        var overlayHidden = sender.getAttribute('placeholderHidden');
        if (overlayHidden != '1') {
            sender.setAttribute('placeholderHidden', '1');
            sender.value = "";
            sender.style.color = "#000000";
        }
    }
</script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜