Reputation: 417
I have a web form with several text boxes. I want to evaluate the default value in a text box and when user clicks on the field I want to clear it out. This is helping to reduce space on a form.
The below gives error message "Microsoft JScript Runtime error: object required.
Javascript:
function validateKeyword(f, flg, val) {
switch (flg) {
case 0: if (document.getElementById(f).value == val) { document.getElementById(f).value = ''; } break;
case 1: if (document.getElementById(f).value == '') { document.getElementById(f).value = val; } break;
}
}
ASP.NET textbox:
<asp:TextBox maxlength="150" runat="server" name="First_Name" value="First Name" class="frmsel2" onfocus="javascript:validateKeyword('first_name',0,'First Name');" onblur="javascript: validateKeyword('first_name',1,'First Name');" alt="First Name" title="First Name" id="first_name"></asp:TextBox>
Upvotes: 0
Views: 89
Reputation: 417
I ended up putting this in the Page_Load event in the code behind. (Made edits to use constant values).
public partial class _Default : System.Web.UI.Page
{
private const string FormDefaFirstName = "First Name";
private const string FormDefaLastName = "Last Name";
protected void Page_Load(object sender, EventArgs e)
{
first_name.Attributes.Add("onfocus", "javascript: if(this.value=='" + FormDefaFirstName + "') this.value='';");
first_name.Attributes.Add("onfocusout", "javascript: if(this.value=='') this.value='" + FormDefaFirstName + "';");
last_name.Attributes.Add("onfocus", "javascript: if(this.value=='" + FormDefaLastName + "') this.value='';");
last_name.Attributes.Add("onfocusout", "javascript: if(this.value=='') this.value='" + FormDefaLastName + "';");
if (!Page.IsPostBack)
{
first_name.Text = "First Name";
last_name.Text = "Last Name";
}
}
Upvotes: 0
Reputation: 121
If you are using HTML5 I think you can use placeholder.
http://www.w3schools.com/html5/att_input_placeholder.asp
Upvotes: 1
Reputation: 2344
reference your ID in javascript by using
document.getElementById(<%= first_name.ClientID %>).value = ''
Upvotes: 0