0

I am very new to jQuery so I am sorry in advance for the ease of this question. I have a page with several text boxes. I am using the selector shown in the code below to get all of the text boxes on the page. I am assuming this selector returns an array of text boxes. I want to clear all of those text boxes. In order to do this I set its val to an empty string. However I thought I would need to use the each method because I am dealing with an array. It appears to work without the each method. Why is this? Does jQuery automatically know to apply the val to every element within the selected array?

$(":text").val("");

2 Answers 2

3

Yes, jQuery automatically apply all your expression to all of the selected elements. Method each in jQuery used, when you need execute some custom function with all selected elements (not standart function). When you use standart functions, you don't need use each method. However, you can rewrite your code like this:

$(":text").each(function() { $(this).val(""); });

But you realize, that this is not pretty sight.

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

Comments

0

empty all textboxes;

$("input:text").val("");

Empty text boxes with certain class;

$("input:text.classname").val("");

clear text boxes with array; assume that you have text box names text1, text2, text3, ....

for (var i = 1; i <= 10; i++){
  $("#text" + i).val('');
}

also you can use many methords using filter and each functions as well.

$("input:text").filter(":visible").filter(function(){
  return this.value !== ""; // you can return with any value
}).each(function(){
  something get sum or anything;
});

even without filter you can use each

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.