PDA

View Full Version : how to reset value of input text fields


skateme
09-22-2008, 07:56 PM
I want to replace the value of an input text field to 0 (nothing) if the user clicks on the field or if the user clicks out of the field. I already have this code going, but I want to prevent the code from changing the input's value if the user has modified the contents of the input. I have this code so far:


<html>
<head>
<style>
function changevalue(){
if(document.loginform.test.value == 'test'){
document.loginform.test.value = "";
}
</style>
</head>
<body>
<div class="loginform">
<input type="text" value="test" id="test" name="test" onclick="this.value=''" onchange="" />
</div>
<div onclick="test.value='test'" style="width:100%;height:100%; position: absolute;">
</div>
</body>
</html>


Let me know if any clarification is needed :) Thanks in advance!

Clueful
09-22-2008, 10:16 PM
I want to replace the value of an input text field to 0 (nothing) if the user clicks on the field or if the user clicks out of the field. I already have this code going, but I want to prevent the code from changing the input's value if the user has modified the contents of the input. Input fields have a defaultValue property.

skateme
09-23-2008, 05:54 AM
Sorry I'm not following you here. I already have a value set for the input. I want to put something inside the onchange tag so that it prevents the next onclick from executing.

Clueful
09-23-2008, 06:04 AM
Sorry I'm not following you here. I already have a value set for the input. I want to put something inside the onchange tag so that it prevents the next onclick from executing.You don't prevent the handler from executing, you prevent it from having any effect under certain conditions, as you have done already.

skateme
09-23-2008, 08:15 PM
Well its not really doing what I want it do. Any advice?

rangana
09-23-2008, 11:35 PM
Hope this basic example helps:

<script type="text/javascript">
function changeVal(el)
{
var defText = 'test'; // Set the default text for comparison to the element's value
el.value=(defText==el.value.toLowerCase())?'':el.value;
}
</script>

<input type="text" value="test" id="test" name="test" onfocus="changeVal(this)" onblur="changeVal(this)" />


... The input's value will only be removed once it is equal to the "default" text value which as of now is test.

Hope that makes sense.