47

How can I clear a text input on click of a button using angularJS?

Search input

The X is a seperate link, on which I would like to trigger a function.

HTML

<input type="text" class="form-control" data-ng-model="searchAll">
<a class="clear" data-ng-click="clearSearch()">X</a>
1
  • 2
    Add your HTML of the input please. Commented Feb 11, 2014 at 17:24

8 Answers 8

87

Just clear the scope model value on click event and it should do the trick for you.

<input type="text" ng-model="searchAll" />
<a class="clear" ng-click="searchAll = null">
    <span class="glyphicon glyphicon-remove"></span>
</a>

Or if you keep your controller's $scope function and clear it from there. Make sure you've set your controller correctly.

$scope.clearSearch = function() {
    $scope.searchAll = null;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works, but only if the code is inline and not in a seperate JS file. Could you tell me how you could make it work in a seperate JS file?
See my edit. But why would you want to add additional code that doesn't require it?
earlier I were using $scope.searchAll = ""; Well it were also working,,,there were some other scenario happening...overall thanks for suggesting null will also work.
15
$scope.clearSearch = function () {
    $scope.searchAll = "";
};

http://jsfiddle.net/nzPJD/

JsFiddle of how you could do it without using inline JS.

Comments

14

An easier and shorter way is:

 <input type="text" class="form-control" data-ng-model="searchAll">
 <a class="clear" data-ng-click="searchAll = '' ">X</a>

This has always worked for me.

1 Comment

when we click on "X" then the model value of "searchAll" will become empty because we have assigned empty string to that model when we click on "X" . !!data-ng-click="searchAll = ' ' "!!. Good about this is we don't have to make and call a separate function for just one line of code.
8

If you want to clean up the whole form, you can use such approach. This is your model into controller:

    $scope.registrationForm = {
    'firstName'     : '',
    'lastName'      : ''
};

Your HTML:

<form class="form-horizontal" name="registrForm" role="form">
   <input type="text" class="form-control"
                       name="firstName"
                       id="firstName"
                       ng-model="registrationForm.firstName"
                       placeholder="First name"
                       required> First name
   <input type="text" class="form-control"
                       name="lastName"
                       id="lastName"
                       ng-model="registrationForm.lastName"
                       placeholder="Last name"
                       required> Last name
</form>

Then, you should clone/save your clear state by:

$scope.originForm = angular.copy($scope.registrationForm);

Your reset function will be:

$scope.resetForm = function(){
    $scope.registrationForm = angular.copy($scope.originForm); // Assign clear state to modified form 
    $scope.registrForm.$setPristine(); // this line will update status of your form, but will not clean your data, where `registrForm` - name of form.
};

In such way you are able to clean up the whole your form

1 Comment

I do implement your solution and work´s for me, i do reveive a warning : TypeError: Cannot read property '$setPristine' of undefined at Scope.$scope.resetForm can i fix this warning ?
5

Easiest way to clear/reset the text field on click is to clear/reset the scope

<input type="text" class="form-control" ng-model="searchAll" ng-click="clearfunction(this)"/>

In Controller

$scope.clearfunction=function(event){
   event.searchAll=null;
}

Comments

1

Inspired from Robert's answer, but when we use,

ng-click="searchAll = null" in the filter, it makes the model values as null and in-turn the search doesn't work with its normal functionality, so it would be better enough to use ng-click="searchAll = ''" instead

1 Comment

Can you speak of the XSS implications when using this method?
0

In Your Controller

$scope.clearSearch = function() {
     $scope.searchAll = '';
}

Comments

0

Try this,

 this.searchAll = element(by.xpath('path here'));
 this.searchAll.sendKeys('');

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.