Reputation: 604
The script code below makes ghost text
$('.ghost-text').each(function(){
var d = $(this).val();
$(this).focus(function(){
if ($(this).val() == d){
$(this).val('').removeClass('ghost-text');
}
});
$(this).blur(function(){
if ($(this).val() == ''){
$(this).val(d).addClass('ghost-text');
}
});
});
But on submit it passes that default ghost value. How to delete it on submit?
Upvotes: 0
Views: 430
Reputation: 74410
If you call 'ghot text' watermark and able to use HTML5, use placeholder attribute instead:
<input type="text" placeholder="My ghost text" />
If 'ghost text' as nothing to do with watermark and dont want to submit it, you can use this instead:
$('form').submit(function(){
$('.ghost-text',this).each(function(){
$(this).removeAttr('name');
});
});
Upvotes: 0
Reputation: 12693
I believe its better to use a placeholder:
<input type="text" id="myTxt" placeholder="enter something..."/>
Or if you want to stick with your js:
if($.browser.msie){
$('.ghost-text').each(function(){
var d = $(this).attr('placeholder');
$(this).focus(function(){
if ($(this).val() == d){
$(this).val('').removeClass('ghost-text');
}
});
$(this).blur(function(){
if ($(this).val() == ''){
$(this).val(d).addClass('ghost-text');
}
});
});
$('form').submit(function(){
$('.ghost-text').each(function(){
if ($(this).val() == $(this).attr('placeholder'))
$(this).val('');
});
});
}
Upvotes: 2
Reputation: 1422
var ghostTexts = $('.ghost-text');
ghostTexts.each(function(){
var $this = $(this),
d = $(this).val();
$this.on({
'focus': function(){
if ($this.val() == d){
$this.val('').removeClass('ghost-text');
}
},
'blur': function() {
if ($this.val() == ''){
$this.val(d).addClass('ghost-text');
}
}
});
});
$('yourForm').on('submit', function() {
ghostTexts.val('');
});
Upvotes: 0
Reputation: 897
Have you tried something like this?
$("#submit").click(function(){
$(".ghost-text").each(function(){
$(this).val('');
}
})
Upvotes: 0