0

I am using Asp.net MVC 4 with C#. I am using a Text box, Drop down and checkbox altogether in a page as a search filter. I am using a button clear to clear the values.while clicking clear button I need to clear the value in textbox, show the "select here" on dropdown(My dropdown values are "Select here", "Value1", "Value2") and uncheck the check box. How to write the script function? My coding is below..

     <div class-="container">
 <div class="row">
<div class="col-xs-4 col-md-4">
<label>Id</label>                                                           @Html.TextBoxFor(model => model.Id, new { @class = "form-control" })
</div>
<div class="col-xs-4 col-md-4">
<label>Role</label>
@Html.DropDownList("SelectRole", new SelectList(@ViewBag._RViewBag, "Key", "Value", Model.Role), new { @class = "form-control" })
 </div>
<div class="col-xs-4 col-md-4">
<br />
@Html.CheckBoxFor(model => model.Status)&nbsp;&nbsp;<label style="font-size:large">  Status</label></div>
</div>
</div>

<div class="col-md-4 btn-top">
<a href="#" class="btn btn-suucess btn-block">Search</a></div>
 <div class="col-md-4 btn-top"><a onclick="Clear()" class="btn btn-success btn-block">Clear</a></div>

Can Anyone help me write the Clear Function?

7
  • 2
    The simple way is to put the fields inside form and add a buttonof type reset Commented Jun 23, 2017 at 6:20
  • I have been a web developer for years. This is the first I've ever heard of reset type. Mind blown. Also, you should create an answer, instead of a comment, because your comment appears to directly answer his question. Commented Jun 23, 2017 at 6:22
  • If u didn't heard about reset yet then you are not a good developer :) Commented Jun 23, 2017 at 6:24
  • While I greatly appreciate your insult, resetting forms has never been a priority for stuff I do. Commented Jun 23, 2017 at 6:26
  • didn't know that neither, good point Commented Jun 23, 2017 at 6:29

1 Answer 1

2
var Clear = function() {
    $('input[type="text"]').val(""); //clears text boxes
    $('input[type="checkbox"]').prop('checked', false); //unchecks checkboxes
    $('select').val("Select here");
}

If I understand correctly, this should do what you need.

Also, as I just learned from the first to comment on your question, you can have a button of type reset if all your inputs are in a form.

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

6 Comments

sir, my dropdown already have the "Select here" text in it.. so I need to hardcode those text inside the function or else any other option to display its first index?
Also, I declared onclick = Clear() ; so that can I write it as a function or declare as a var ? if variable means, where to call that one?
The way I've always handled setting default options in selects, is to set the value of that option to "" (value="") in the html, and then you can just set it's value in jquery by $('select').val("")
And you can either use var Cear = function() or function Clear(). For your purposes they'll work the same.
Sir Your Coding working for both drop down and check box, but for the text box its not working sir...
|

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.