Reputation: 387
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
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
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
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
Reputation: 1821
Here'e the code:
$('button').click(function(){
$('input[type="text"]').val('0');
});
That's it.
Upvotes: 0
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
Reputation: 25882
No need of saying each, just say .click
it will apply for every button
$('button').click(function(){
$('input[type="text"]').val(0);
});
Upvotes: 2
Reputation: 5211
Try this.
$('button').click(function(){
$('input[type="text"]').val('');
});
Upvotes: 0