2

I have defined my form elements using the html array method like this:

<input type="text" maxlength="45" name="Apikey[key]">
<select name="Apikey[developer]">

How can I clear all Apikey array using jQuery?

The question is how to clear them? All possible forms fields must be cleared.

4
  • The question is to reset them? That's just setting $("el").val("");. But you have special characters in your jQuery selection string which will fail. Commented Jan 27, 2011 at 22:25
  • select elements don't go back when using val("") Commented Jan 27, 2011 at 22:26
  • You haven't asked about select elements. Commented Jan 27, 2011 at 22:27
  • I don't want a reset. I want all form elements to have no value. Checkboxes cleared, selects to -1 etc... Commented Jan 29, 2011 at 18:10

5 Answers 5

3

Try ^(starts with) in the selector instead of |. Also to reset the fields appropriately storing the initial values using data. i.e:

$(function(){
    $("input[name^=Apikey]").each(function(){
      var $this = $(this);
      $this.data("initial-val", $this.val());
    });
});
.
.
.
function resetApiValues(){
   $("input[name^=Apikey]").each(function(){
     var $this = $(this);
     $this.val($this.data("initial-val"));
   });
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is a good aproach as a selector, but doesn't mentioned how to clear them.
Updated the post to reset the value for filtered fields.
As I mentioned in the questions's comment section I don't need the reset function, I don't need to save the initial values. I would like to clear all filters.
The above code resets the form, that means it puts back all the values as it was when the page loaded initially. Instead I am looking to clear all the form elements, and I don't care about their initial values. The val() doesn't work on checkboxes/radios and selects.
2

The easiest thing to do would be to use the "standard" form plugin and call clearForm() or clearFields():

$('#formId').clearForm();
$('#formId [name^=Apikey]').clearFields();

Or, if you don't want or need all the extra form plugin machinery, just grab the source and figure out how clearForm() and clearFields() work. clearFields() is actually quite simple:

$.fn.clearFields = $.fn.clearInputs = function() {
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        if (t == 'text' || t == 'password' || tag == 'textarea') {
            this.value = '';
        }
        else if (t == 'checkbox' || t == 'radio') {
            this.checked = false;
        }
        else if (tag == 'select') {
            this.selectedIndex = -1;
        }
    });
};

Comments

1

here you go:

$("input[name^='Apikey']").attr("name", "");

to clear the values:

$("input[name^='Apikey']").val("");

I see that you have edited to include different HTML elements, in that case:

$("*[name^='Apikey']")).each(function() { 
    switch(this.type) { 
        case 'password': 
        case 'select-multiple': 
        case 'select-one': 
        case 'text': 
        case 'textarea': 
            $(this).val(''); 
            break; 
        case 'checkbox': 
        case 'radio': 
            this.checked = false; 
    } 
}); 

Comments

0

$('input[name^="Apikey"]')...

See the official jQuery API here. bye

1 Comment

This is a good aproach as a selector, but doesn't mentioned how to clear them.
-1

You cant just clear all the values. Some elements like SELECT have to have a value.

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.