FairyBusiness
asked on
How to undo or default back in jquery?
Hi, I have a function that when click removes the default values of the text input fields:
$('.text').click(function( e) {
e.preventDefault();
$(this).val('');
});
But I want the default values to appear back if the user does not enter anything (click on something else). Anyone know how to accomplish this??
$('.text').click(function(
e.preventDefault();
$(this).val('');
});
But I want the default values to appear back if the user does not enter anything (click on something else). Anyone know how to accomplish this??
<INPUT type="text" ID="Text1" NAME="Text1" onclick="document.getEleme ntById('Te xt1').valu e=''" value="Default"
onBlur ="if (document.getElementById(' Text1').va lue=='') document.getElementById('T ext1').val ue ='Default'" >
In your case
$('.text').onBlur(function (e) {
e.preventDefault();
if ($(this).val.value=='')
$(this).val('Defualt');
});
onBlur ="if (document.getElementById('
In your case
$('.text').onBlur(function
e.preventDefault();
if ($(this).val.value=='')
$(this).val('Defualt');
});
ASKER
What does returning it false do??
nothing
it will display the whatever value is enterd in that
it will display the whatever value is enterd in that
ASKER
Both of your guys function prevents my other function from working:
$('#submit').click(functio n(e) {
e.preventDefault();
var name = $('#name').val();
$("#name2").append(name);
});
Now when I click on a text field the value does not disappear. Is there a way to make the value disappear when you click on it, but if you leave it blank and click away the default value comes back??
$('#submit').click(functio
e.preventDefault();
var name = $('#name').val();
$("#name2").append(name);
});
Now when I click on a text field the value does not disappear. Is there a way to make the value disappear when you click on it, but if you leave it blank and click away the default value comes back??
$('.text').onBlur(function (e) {
if ($(this).val.value=='')
$(this).val('Defualt');
});
if you try this is functions as you said
<INPUT type="text" ID="Text1" NAME="Text1" onclick="document.getEleme ntById('Te xt1').valu e=''" value="Default"
onBlur ="if (document.getElementById(' Text1').va lue=='') document.getElementById('T ext1').val ue ='Default'" >
if ($(this).val.value=='')
$(this).val('Defualt');
});
if you try this is functions as you said
<INPUT type="text" ID="Text1" NAME="Text1" onclick="document.getEleme
onBlur ="if (document.getElementById('
ASKER
Well, I am only using jquery for this and so far nothing as worked. . .
try like this
$('.text').onBlur(function (e) {
if ( $(this).val().length == 0 )
$(this).val('Defualt');
});
$('.text').onBlur(function
if ( $(this).val().length == 0 )
$(this).val('Defualt');
});
ASKER
Nope, that didnt work either. . .
They disappear but do not come back
http://auroriella.com/story.html
They disappear but do not come back
http://auroriella.com/story.html
Hi Fairy,
follow the code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script language="JavaScript" type="text/javascript" src="jquery-1.4.1.js"></sc ript>
<script language="JavaScript" type="text/javascript">
$(function() {
$("#txtOne").click(functio n(e) {
if ($(this).val() == "default value")
$(this).val("");
return false;
});
$("#txtOne").focusout(func tion(e) {
if ($(this).val() == "")
$(this).val("default value");
return false;
});
});
</script>
</head>
<body>
<div style="width: 50%">
<input type="text" id="txtOne" value="default value" />
</div>
</body>
</html>
follow the code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script language="JavaScript" type="text/javascript" src="jquery-1.4.1.js"></sc
<script language="JavaScript" type="text/javascript">
$(function() {
$("#txtOne").click(functio
if ($(this).val() == "default value")
$(this).val("");
return false;
});
$("#txtOne").focusout(func
if ($(this).val() == "")
$(this).val("default value");
return false;
});
});
</script>
</head>
<body>
<div style="width: 50%">
<input type="text" id="txtOne" value="default value" />
</div>
</body>
</html>
ASKER
this did not work for me:
$(function() {
$(".text").click(function( e) {
if ($(this).val() == "default value")
$(this).val("");
return false;
});
$(".text").focusout(functi on(e) {
if ($(this).val() == "")
$(this).val("default value");
return false;
});
I still wish you would tell me why you put return false at the end. I never know when to put return false, return true, or nothing. . .
$(function() {
$(".text").click(function(
if ($(this).val() == "default value")
$(this).val("");
return false;
});
$(".text").focusout(functi
if ($(this).val() == "")
$(this).val("default value");
return false;
});
I still wish you would tell me why you put return false at the end. I never know when to put return false, return true, or nothing. . .
oh, it's just preventing default !
try this
refer for more info
http://coldfusioncode.adampresley.com/jquery-examples/defaultIfNull.cfm
$('.text').onBlur(function (e) {
$(this).val = ($(this).val()|| "Default"));
});
refer for more info
http://coldfusioncode.adampresley.com/jquery-examples/defaultIfNull.cfm
$('.text').onBlur(function
$(this).val = ($(this).val()|| "Default"));
});
have you created html page with what i have sent in my previous comment, if not try doing that and you will understand the logic.
$('.text').onBlur(function (e) {
$(this).val = ($(this).val()|| "Default"));
return true;
});
$(this).val = ($(this).val()|| "Default"));
return true;
});
ASKER
Ok, pratima I tried that last one but it didnt work. I will try your script tomorrow jainnaveen. But I'm about to pass out, so tired. Never thought this would be so confusing. . .
take it easy ! anyway I am off this easter holidays. Happy Easter
Revised version, Have used this in of my works...
pls change the jquery src path that of your
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Text Box - default text</title>
<script language="JavaScript" type="text/javascript" src="jquery-1.4.1.js"></sc ript>
<style type="text/css">
.text {}
</style>
<script language="JavaScript" type="text/javascript">
$(function() {
$(".text").click(function( e) {
if ($(this).val() == "default value") {
$(this).val("");
$(this).css({ color: 'Black' })
return false;
}
return true;
});
$(".text").focusout(functi on(e) {
if ($(this).val() == "") {
$(this).val("default value");
$(this).css({ color: 'Gray' })
return false;
}
return true;
});
});
</script>
</head>
<body>
<div style="width: 50%; text-align:center">
<input type="text" class="text" id="txtOne" style="color:Gray " value="default value" />
</div>
</body>
</html>
pls change the jquery src path that of your
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Text Box - default text</title>
<script language="JavaScript" type="text/javascript" src="jquery-1.4.1.js"></sc
<style type="text/css">
.text {}
</style>
<script language="JavaScript" type="text/javascript">
$(function() {
$(".text").click(function(
if ($(this).val() == "default value") {
$(this).val("");
$(this).css({ color: 'Black' })
return false;
}
return true;
});
$(".text").focusout(functi
if ($(this).val() == "") {
$(this).val("default value");
$(this).css({ color: 'Gray' })
return false;
}
return true;
});
});
</script>
</head>
<body>
<div style="width: 50%; text-align:center">
<input type="text" class="text" id="txtOne" style="color:Gray " value="default value" />
</div>
</body>
</html>
PS:
http://www.miuaiga.com/index.cfm/2009/8/31/jQuery-form-input-defaultValue
if ($(this).val() == "") {
$(this).val($(this)[0].def aultValue) ;
}
http://www.miuaiga.com/index.cfm/2009/8/31/jQuery-form-input-defaultValue
if ($(this).val() == "") {
$(this).val($(this)[0].def
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You might consider something like the following:
Make a list of your input elements and what their default values should be.
Why? I have had trouble with default values "disappearing"; the property just seems unreliable.
Then attach an event handler to the window onclick event, and spin through the list, stuffing default values as needed.
It isn't JQuery, but you can adapt it, if you wish.
Make a list of your input elements and what their default values should be.
Why? I have had trouble with default values "disappearing"; the property just seems unreliable.
Then attach an event handler to the window onclick event, and spin through the list, stuffing default values as needed.
It isn't JQuery, but you can adapt it, if you wish.
<script type="text/javascript">
// Use an anonymous function (do not pollute the global namespace)
(function() {
var list = ["input_1", "ABC", "input_2", "DEF", "input_3", "GHI"];
var x;
function window_check() {
for (var i = 0; i < list.length; i += 2) {
x = document.getElementById(list[i]); // Reference the input
if (x.value == "") { x.value = list[i + 1]; } // Reset the default value
}
return false;
}
window.onclick = window_check;
})(); // Invoke the function
<script>
ASKER
This worked on the first try and its in jquery, which is what I was needing. Thank you to everyone who contributed, I learned from other posts as well. Please don't be shy about posting on my questions in the future!
$("#txtOne").focusout(func
if ($("#txtOne").val() == "")
$("#txtOne").val("default value");
return false;
})
});