Question:
(javascript) How can I replace an empty text field with its' original value "onchange"?
Ian
2012-02-02 21:38:07 UTC
In a form I have a text input for the user's first name, and have successfully gotten the "onclick" event to work (which clears the default text value onclick).

I have been trying for hours to restore the default text value if "onchange" the text field is still emtpy... Please help?! Here is my html:



...and here is my Javascript:

function clearTextField(text)
{
text.value = "";
}

function checkIfBlank(text)
{
if(text.value == "")
text.value = this.value;
}

Or are you not allowed to call 2 different Javascript events in the same tag?
Four answers:
fantabulum
2012-02-02 22:53:05 UTC
OnClick = 'clearTextField(this)'

OnBlur = 'function(){ if(this.value===""){ this.value = this.defaultValue; }}'



--or if you prefer--



function checkIfBlank(text){

if(text.value === ""){ text.value = text.defaultValue; }

}



You can have as many event handlers in a tag as you'd like; as long as they don't conflict with one another (in this case they don't). Your problem is with the scoping of 'this' keyword. Input values all have a default value, set it back to that if it's blank. Using 'onchange' as you did will work but OnBlur will check every time they leave the input field, which is what I'd do.
anonymous
2015-02-01 13:21:51 UTC
When getting rid of default text, don't use onclick because it is annoying as each time you click inside the input field, the text automatically disappears, imagine if your client is filling in a form and their details are cleared each time they click in box?



To resolve this issue, use the onkeydown event instead so that the text only disappears when you enter a key and on the condition that the input field contains certain text, then when you click outside of it, the default values are returned on the condition that the input field is empty, for example:



cansler
2016-10-21 03:54:02 UTC
the final public of yankee "Christian" church homes and their attenders are so a strategies from genuine Christianity that they shouldn't additionally be waiting to declare to be Christian. They pontificate this type of comfortable, sugar lined gospel that early believers could get sick from all the candy that passes for preaching in the present day. human beings in the present day want the fact and maximum can see remarkable via a liar so as that they are leaving the mainline "candy production facility" denominations and finding for the genuine deal. regrettably even numerous Non-denominational and so observed as pentecostal preachers have additionally fallen into uncomplicated-believeism so the human beings don't be attentive to the place to coach, maximum of incorporate turning their backs on God and leaving the church. regrettably by way of fact of fake doctrines like eternal risk-free practices, a lot of people who're leaving are doing so thinking that on account that they as quickly as suggested a prayer, they're nevertheless going to heaven, even however they now not persist with God's be conscious. The Bible suggested this could take place, that interior the final of the final days, there'll be a great falling faraway from the religion. So this comes as no ask your self to those people who be attentive to the Lord and His be conscious.
nicefx
2012-02-02 22:31:46 UTC
try use different approach :

first, in onFocus event i store original value to a global variable.

second, i use onBlur event to restore original value which store in the global variable to textbox.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...