Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  24] [ 7]  / answers: 1 / hits: 17972  / 12 Years ago, tue, june 26, 2012, 12:00:00

Placeholder attribute shown below works fine in firefox but if val() is called when the field is empty it returns the placeholder value instead of the actual value in the text.



JSFiddle - http://jsfiddle.net/Jrfwr/2/



<input id=tlt type=text placeholder=Enter Title />


JSCode



function placeHolderFallBack() {
if (placeholder in document.createElement(input)) {
return;
}
else {
$('[placeholder]').focus(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function () {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function () {
$(this).find('[placeholder]').each(function () {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
}
}

More From » jquery

 Answers
8

You could override the val() method but I don't like doing that :D



I wrote a simple pVal() function which does the job



$.fn.pVal = function(){
var $this = $(this),
val = $this.eq(0).val();
if(val == $this.attr('placeholder'))
return '';
else
return val;
}
$(function(){
alert($('input').val())
alert($('input').pVal())
});​


http://jsfiddle.net/W7JKt/3/


[#84658] Monday, June 25, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mahogany

Total Points: 645
Total Questions: 107
Total Answers: 98

Location: Israel
Member since Wed, Apr 14, 2021
3 Years ago
;