deviloper
deviloper

Reputation: 7240

how to get the values of all elements with the same class

I have a bunch of input text fields that are in the same class emps_l with different values. I want to loop through the all elemets with that class and store the values in to an array! Bellow is what I have done:

var emps = new Array();     
$.each(($(".emps_l").val()),function()({        
      emps.push($(".emps_l").val());        
});
console.log(emps);

I am totally lost, Any help will be highly appreciated!

Upvotes: 0

Views: 210

Answers (3)

Gurpreet Singh
Gurpreet Singh

Reputation: 21233

var emps = []; // This is considered slightly faster than new array

$('.emps_1').each(function(){
    emps.push(this.value);
});

console.log(emps);

Upvotes: 1

sino
sino

Reputation: 766

use this :

var emps = new Array();     
$.each(($(".emps_l:input").val()),function(index , item )({        
      emps.push($(item ).val());        
});
console.log(emps);

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

You can use .map() along with this.value to create the array

var emps = $(".emps_l").map(function () {
    return this.value
}).get()

Upvotes: 4

Related Questions