2

How to reset <INPUT> and <TEXTAREA> values with jquery click() if these elements are not part of any <FORM>?

3
  • Would be great if you can share some code. Commented Dec 13, 2013 at 12:16
  • 2
    you can just use the id of the elements to reset it. I think for input it will be $(#id).val('') for textarea $(#id).html('') Commented Dec 13, 2013 at 12:17
  • possible duplicate of Clear text area Commented Dec 13, 2013 at 12:27

6 Answers 6

3

You can reset the values using defaultValue property, both HTMLTextAreaElement and HTMLInputElement support the property, the default value is the value as originally specified in HTML that created these objects.

$('#reset').on('click', function() {
   $('input, textarea').val(function() {
       return this.defaultValue;
   });
});

In case that you only want to reset the values of inputs and textareas that don't belong to a form element you can use .filter() method:

$('#reset').on('click', function() {
   $('input, textarea').filter(function() {
       return !this.form;
   }).val(function() {
       return this.defaultValue;
   });
});

If the form property is null the input/textarea is not descendant of a form element.

http://jsfiddle.net/E2tbk/

Sign up to request clarification or add additional context in comments.

2 Comments

@Sergio I was updating the answer. The second snippet covers this.
@undefined, ok, great!
1

This works:

var elements = $('input,textarea');
$('button').click(function () {
    elements.each(function () {
        if (!this.form) this.value = this.defaultValue;
    });
})

Fiddle

Comments

0

use this:

document.getElementById('yourInputID').value = "";

Comments

0

This solution stores the initial values in the data-oldvalue attribute of the element. When you click the restore button it sets the values back to the initial ones.

Some pointed out you should use .html() to set the value of the textarea. I tried this but it did not set the value while .val() did

$("input").each(function() {   
    $(this).attr("data-oldvalue", $(this).val());
});

$("textarea").each(function() {
    $(this).attr("data-oldvalue", $(this).html());
});


$("button").click(function() {

    console.log($(this).attr("data-oldvalue"));

    $("input").each(function() {   
        $(this).val($(this).attr("data-oldvalue"));     
    });

    $("textarea").each(function() {
        $(this).val($(this).attr("data-oldvalue"));    
    });

});

See: http://jsfiddle.net/zvPK7/

4 Comments

No, not erase. I had some value before edit, than after click old value must be returned.
I dont think $("textarea").val(""); will work. But I may be wrong .. you need .html('') here.
The you first need to store all those values beforehand and then restore them I will create an example.
use some hidden variables.. store the values there and then again restore them.
0

Try this:

$('#click').on('click', function() {

    $("input[type='text'], textarea").val('');
});

Comments

-1

Try This...

$('#btnID').click(function(){
        $('#FormID')[0].reset();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.