I have multiple text box like this. I want to apply the text on blur and empty when it get focused. I can able to achieve this for single text box. How i can pass the current element id instead of the hard coding the “#name” value in JavaScript function ?
$(document).ready(function(){
$('#Name').focus(function()
{
var $ele = $(this).attr('id');
$(this).val('');
$(this).css('color', '#000000');
});
$('#Name').blur(function()
{
var $ele = $(this).attr('id');
$(this).val($ele);
$(this).css('color', '#a9a9a9')
});
});
<input id="Name" type="text" value="" name="Name">
<input id="Phone" type="text" value="" name="Phone" >
<input id="Email" type="text" value="" name="Email">
You can select all input by
$('input')and you can put filter text inputs using[type="text"]; You can take benefits of jQuery chaining.You can directly set value of input fields in html
OR Apply default values on page load as:
HTML