user1178399
user1178399

Reputation: 1118

Javascript: how to clear textboxes and restore their values?

I have dozen textboxes on my form. On button click I need to clear some of them (not all) and if some conditions are satisfied, I need to restore values in textboxes. How can I do that with less blood?

Upvotes: 1

Views: 371

Answers (2)

user1432124
user1432124

Reputation:

This will manipulate your all input depending on condition but you have to call this method on your own

  $("form").children("input").each(function(){
    if(condition)
    {$(this).val(this.defaultValue);
    }
    })

Upvotes: 1

Teneff
Teneff

Reputation: 32148

There is an .defaultValue property which you can use like this:

txt.onblur = function() {
    alert('restoring default value!');
    this.value = this.defaultValue;
}
​

jsFiddle example

Upvotes: 0

Related Questions