Vineet
Vineet

Reputation: 387

Clear Input Fields on button click

I have got a input field where in user enters data. There are 2 buttons next to it upon click any one of them or both the input fields get reset. Now since these buttons and field is not inside a form I can't go for a instead how can i clear it with JQuery. The point is, now I have displayed only 2 buttons, the JQuery must also work if there where more buttons..Fiddle link below and the code i tried

$('button').each(function(){
   $('button').click(function(){
      find('input[type="text"]').val() = "0";
   });
});

[Fiddle link] http://jsfiddle.net/vineetgnair/1s22gcL5/

Thanks to all for help.

Upvotes: 4

Views: 26209

Answers (8)

Kartikeya Khosla
Kartikeya Khosla

Reputation: 18883

This will work:

$('button').click(function(){
   $('input[type="text"]').val(0);
});

If you want to just reset field then :

$('button').click(function(){
   $('input[type="text"]').val('');
});

Upvotes: 10

VPK
VPK

Reputation: 3090

You can use following code to clear your input field on button click.

$('button').each(function(){
    $(this).click(function(){
        $('input[type="text"]').val('');
    });
});

Upvotes: 0

Punitha Subramani
Punitha Subramani

Reputation: 1477

<input type="reset" value="button 2" />

Upvotes: 0

Klapsius
Klapsius

Reputation: 3369

This clean text field:

 $('button').click(function(){
        $('input[type="text"]').val("");
    });

This change value to 0:

 $('button').click(function(){
        $('input[type="text"]').val(0);
    });

Upvotes: 1

Viswanath Donthi
Viswanath Donthi

Reputation: 1821

Here'e the code:

$('button').click(function(){
   $('input[type="text"]').val('0');
});

That's it.

Upvotes: 0

mk117
mk117

Reputation: 775

Instead of javascript variable defining to jQuery.function = 0, you should just reset the value to clear the contents of the input field with .val('');

$('button').each(function(){
    $('button').click(function(){
        $('input[type="text"]').val('');
    });
});

Upvotes: 1

Mritunjay
Mritunjay

Reputation: 25882

No need of saying each, just say .click it will apply for every button

$('button').click(function(){
      $('input[type="text"]').val(0);
});

DEMO

Upvotes: 2

RGS
RGS

Reputation: 5211

Try this.

$('button').click(function(){
   $('input[type="text"]').val('');
});

Upvotes: 0

Related Questions