0

hi i made a tutorial for appearing default text in text fields

[snipped blog promotion]

Edited by Ezzaral: n/a

4
Contributors
7
Replies
9
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

This way is easier, no jQuery required. ;)

<html>

<head>

</head>

<body>

<form>
Email: <input type="text" id="txtEmail" value="default@email.com" />

</form>

</body>

</html>
0

this way is totally wrong

1.Because you given value to the field not the default text.

2. default text is about to appear as for the ease for the user and when they click on the field that text disappear while what you did you just given a value to the field.look at the link above and you will experience that what is it about

0

I understand that JQuery is a nice library. Though I could use pure JavaScript to do the same job even though it would be longer due to JQuery hides most of the code for you.

However, it seems that your code has a flaw. What do you think if I am not using an updated browser and enter only white spaces in the field? One should not assume that all users have the same computer knowledge...

0

OK, um ... guess you didn't realize I was just making a joke, but that's fine.

You can improve your tutorial by setting a custom attribute on the input in order to make it so that you don't need to define a variable for default values.

<html>

<head>

<script src="jquery-1.6.4.min.js"></script>


<script>

$(function(){
	$('#txtEmail').val($('#txtEmail').attr('data-default'));

	$('#txtEmail').focus(function(){
		if($(this).val() === $(this).attr('data-default')){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val().length === 0){
			$(this).val($(this).attr('data-default'));
		}
	});
});

</script>

</head>

<body>

<input type="text" id="txtEmail" data-default="email@address.com" />

</body>

</html>
0

this is a tutorial for having default text and if i have used some short path its all for the sake of tutorial guys..all you need to known is how we can apply the defalut text specially this tutorial for type email to them and dont think it has any flaw regarding the code

0

Hmm... So you don't have a though of older version of a browser that does not accept HTML5? The purpose of your script is supposed to "redisplay" the text if the field is empty. However, if the type of "email" does not apply because it is not HTML5 and a user enter 1 space, it would look empty and by pass your check. If you are giving a tutorial, it is fine. However, you need to explain what your tutorial does not offer or those who want to learn need to look for in the future.

Votes + Comments
No reason for negative rep on this post.
irritating
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.