Watermark textbox in HTML
Introduction: Hello Guys, “Welcome Back Again” Today i came here with
one more new great article.
In this article I will explain about to create Watermark textbox in HTML.
Here I am using HTML placeholder variable that is a new variable in
HTML5 version. It was not in previous versions of HTML. So HTML placeholder
variable will only work in latest browsers that support HTML5.
Firstly create a text box that you wish to add a place holder too. In this example we will create text of for entering Name. Now we need to add the code for the placeholder variable for HTML5 browsers. As you can see we have added a new variable inside <input>. You can enter any value inside the double quotes (“”) of placeholder as per your requirement. HTML5 browsers mean only latest browser can use the placeholder variable. We also need to add the jQuery function on the HTML page. Here I am giving the complete working code for HTML page. You need to copy and paste the whole HTML code in your HTML Page for enjoy this code.
<html>
<head>
<title>Watermark Text Box
</title>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<script>
// This adds 'placeholder' to the items listed in the jQuery .support object.
jQuery(function() {
jQuery.support.placeholder = false;
test = document.createElement('input');
if('placeholder' in test) jQuery.support.placeholder = true;
});
// This function will use for support the code for browsers.
$(function() {
if(!$.support.placeholder) {
var active = document.activeElement;
$(':text').focus(function () {
if ($(this).attr('placeholder') != '' && $(this).val() == $(this).attr('placeholder')) {
$(this).val('').removeClass('hasPlaceholder');
}
}).blur(function () {
if ($(this).attr('placeholder') != ''
&& ($(this).val() == '' || $(this).val() == $(this).attr('placeholder'))) {
$(this).val($(this).attr('placeholder')).addClass('hasPlaceholder');
}
});
$(':text').blur();
$(active).focus();
$('form:eq(0)').submit(function () {
$(':text.hasPlaceholder').val('');
});
}
});
</script>
<style>
.hasPlaceholder {
color: #987f3f ;
}
</style>
</head>
<body>
<input type="text" name="txt_name" placeholder="Enter Your Name" />
</body>
</html>
Conclusion: In above code, I have been explained that how we can create textbox with Watermark feature in HTML5. This code is very helpful for every developer. Bye BYE and take care of yourself Developers. We will come back shortly with the new article.
Regards
Using Asp.net
Comments
Post a Comment