`

Default Text Label in Textbox using JavaScript/jQuery

阅读更多

文章源自:http://viralpatel.net/blogs/default-text-label-textbox-javascript-jquery/

Default Text Label in Textbox using JavaScript/jQuery

n one of my previous article about Change Form Textbox Style on Focus, we discussed about Usability and HCI issues. Slight change in web pages/input forms can increase user experience.

Nowadays, it has became a common practice to give user labels on fields that they are editing. For example, if you see Google’s custom search textbox in any website, most of them will have a Google’s Logo in background and when user clicks on Textbox, the logo disappears. Thus we can create an input form with labels in background of textboxes. When user select the textboxes, the labels disappear enabling user to edit the textbox.

Let us see how can we use JavaScript/jQuery to achieve this.

Step 1: The HTML

We will use following simple HTML Form. Create an HTML file and Copy and paste following code.

<FORM method="post">
<TABLE>
<TR>
    <TD>Username</TD>
    <TD><INPUT type="text" value="" name="username" title="Enter Username"/><TD>
</TR>
<TR>
    <TD>Age</TD>
    <TD><INPUT type="text" value="" name="username" title="Enter Age"/><TD>
</TR>
<TR>
    <TD>City</TD>
    <TD><INPUT type="text" value="" name="username" title="Enter City"/><TD>
</TR>
<TR>
    <TD>Comment</TD>
    <TD><INPUT type="text" value="" name="username" title="Enter Comment"/><TD>
</TR>
</TABLE>
</FORM>

 

Notice that while defining each text fields, we have provided title attribute. The text that is displayed on background of textbox is taken from the Title attribute. So generally the title attribute should be like “Enter Email Address” or “Type Address”.

Step 2: The CSS

Copy following CSS in your HTML file. You may want to include a separate CSS file for this.

.text-label {
    color: #cdcdcd;
    font-weight: bold;
}

 

The above CSS class is applied to the Textbox when user has not entered anything in it. Once the user starts typing in Textbox the CSS class is removed.

Step 3: The JavaScript

We are almost done with our implementation. Only thing left is the JavaScript code that actually add and remove css to textboxes.

$('input[type="text"]').each(function(){
 
    this.value = $(this).attr('title');
    $(this).addClass('text-label');
 
    $(this).focus(function(){
        if(this.value == $(this).attr('title')) {
            this.value = '';
            $(this).removeClass('text-label');
        }
    });
 
    $(this).blur(function(){
        if(this.value == '') {
            this.value = $(this).attr('title');
            $(this).addClass('text-label');
        }
    });
});

 The above code is straight forward. For each textbox in the webpage, we hook handler function to events focus and blur. When Textbox is focused, check if the value is same as Title. If so, then remove the background label css and let user edit the value in textbox. And while blur event(focus lost), check the value: if user has not entered anything, set the background css again.

 



 

 

 

 

 

 

 

 

  • 大小: 2.7 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    Add Textbox Dynamically using jQuery.zip

    "Add Textbox Dynamically using jQuery.zip"这个压缩包中的示例,主要展示了如何利用jQuery在用户交互时动态创建一个新的文本输入框。 首先,jQuery库是一个强大的JavaScript库,它简化了DOM操作、事件处理、动画...

    easyui-textbox和easyui-combobox的onchange事件响应实例

    &lt;link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/easyui/themes/default/easyui.css"&gt; &lt;link rel="stylesheet" type="text/css" href=...

    LabelTextbox复合控件

    LabelTextbox复合控件是一种在软件开发中常见的自定义组件,特别是在UI设计中,它结合了Label和TextBox两种基本控件的功能。Label通常用于显示固定文本,而TextBox则用于用户输入文本。通过将这两种控件组合成一个...

    可用于Label和textbox的日历控件

    即它能够与Label和TextBox进行交互,可能是通过事件触发,如点击Label或TextBox旁边的图标来显示日历,或者通过JavaScript或jQuery实现动态绑定,使得日历控件的选中值能够实时更新到TextBox中。 标签"Label ...

    net组件LabelTextbox源码

    net组件LabelTextbox源码 自行编制 private Label label = new Label();//创建一个标签 private TextBox textBox = new TextBox();//创建一个文本框 private Color _backgroundColor;//鼠标移入的背景颜色 ...

    TestForm(label和textbox组合控件)

    public string LabelText { get; set; } public string TextBoxText { get; set; } ``` 4. **处理事件**:实现特定的行为,如文本改变、点击等,需要重写或添加事件处理程序。例如,当TextBox中的文本发生变化时,...

    Drag and Drop Inserting Text to Input Textbox with jQuery.zip

    在这个"Drag and Drop Inserting Text to Input Textbox with jQuery"的项目中,我们关注的是如何利用jQuery库来实现这一功能,特别是在输入文本框中插入拖放文本的场景。jQuery是一个流行的JavaScript库,它简化了...

    js获取asp.net服务器端控件Label,TextBox,RadioButtonList,DropDownList的值

    对于基于ASP.NET的项目而言,开发者通常需要通过JavaScript来操作服务器端控件的状态,如获取Label、TextBox、RadioButtonList和DropDownList等控件的值。下面将详细介绍如何使用JavaScript来访问这些控件,并提供...

    Wince上显示Label与TextBox组列表

    Wince上显示Label与TextBox组列表。当Label与TextBox组合太多,在一个Wince显示页面上显示不完全的时候,采用该方法。

    labwindows/CVI 文本框(textbox)基础使用方法

    该资源为一个CVI文本框(textbox)写入信息与删除信息的简易示范工程,其中关键代码已经进行了适当的注释。本工程还包含如何在CVI中弹出提示信息,工程实现了以下目标: 1.ok按钮按下后在textbox上显示固定的信息 ...

    LabelTextBox.rar

    在给定的“LabelTextBox.rar”压缩包中,我们很可能找到了一个自定义控件,它将基本的Label和TextBox控件组合在一起,形成了一个集成的输入和显示组件。这个控件可能是为了方便在窗体设计时同时显示提示信息和获取...

    asp.net中TextBox获得焦点和失去焦点——客户端JavaScript事件

    &lt;script type="text/javascript"&gt; $(function () { $('#&lt;%= TextBox1.ClientID %&gt;').on('blur', function () { var input = $(this).val(); PageMethods.ValidateInput(input, OnSuccess, OnError); }); }); ...

    适用于ERP的TextBox/COMB/DTPICK控件及漂亮的容器控件

    输入框控件: 将标题与输入框整合在一起,并且可以设置不可为空以及判断主键重复的属性,以及类似金蝶输入框的漂亮样式。 容器控件: 可拖动容器,可设置左右图标,多样式颜色设置等

    ActiveReport动态生成label控件和textbox控件

    本文将详细介绍如何在ActiveReport中动态创建Label和TextBox控件,适用于那些需要根据数据库数据自动生成报表结构的情况,例如工资报表,其组成部分可能需要从数据库中动态获取。 首先,我们需要了解ActiveReport...

    listview内嵌textbox(c#)

    item.SubItems[0].Text = textBox.Text; // 假设第一列是可编辑的 } ``` 此外,为了确保数据的一致性,你可能还需要实现撤销和重做功能,以及在失去焦点或按下Enter键时自动提交更改。这可以通过维护一个操作历史...

    C# 禁用textbox后 改变text的颜色 TextBox

    this.ForeColor = SystemColors.ControlText; } } ``` 在上面的代码中,我们创建了一个名为`DisableForeColor`的属性,用于存储禁用状态下的文本颜色。当控件的`Enabled`属性发生变化时,我们调用`...

    asp.net中TextBox获得焦点显示JS日历控件

    本文将详细讲解如何在TextBox获得焦点时显示JavaScript日历控件,为用户带来更好的交互体验。 首先,我们需要一个TextBox控件,用户在输入日期时可以点击它,然后弹出日历供用户选择。在ASP.NET页面中,创建TextBox...

    限制TextBox只能输入整数或者小数

    textBox.Text = textBox.Text.Remove(e.Changes[0].Offset, e.Changes[0].AddedLength); textBox.Select(e.Changes[0].Offset, 0); } } ``` #### 3. 正则表达式的运用 正则表达式是一种强大的文本匹配工具,...

    项目开发asp.net

    &lt;asp:TextBox ID="TextBox2" runat="server" TextMode="Password"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;asp:CheckBox ID="CheckBox1" runat="server" Checked="True" Text="记住密码" /&gt; ...

    asp.net 自定义textbox控件,带有客户端验证

    [DefaultValue(false)] public bool Required { get; set; } ``` 为了实现客户端验证,我们需要利用ASP.NET的AJAX库,特别是Validator控件和JavaScript。我们可以使用ValidationPropertyAttribute指定哪个属性是...

Global site tag (gtag.js) - Google Analytics