Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 390
littleanimboy Avatar asked Dec 09 '22 06:12

littleanimboy


2 Answers

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('');
});
like image 125
Kartikeya Khosla Avatar answered Dec 10 '22 18:12

Kartikeya Khosla


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

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

DEMO

like image 27
Mritunjay Avatar answered Dec 10 '22 19:12

Mritunjay