Tags
Asked 3 years ago
10 Jun 2021
Views 1576
Gino

Gino posted

.text() multiple elements same class in jQuery

how to get text from all multiple element which have same class in jQuery

 <div class="maintmenu">Home</div>
 <div class="maintmenu">Categories</div>
 <div class="maintmenu">Contact us</div>
 <div class="maintmenu">About us</div>


for above html
when i try to use text() from Jquery


$(".mainmenu").text();

it will return all together but i need it complete seprate ,

steave

steave
answered Jun 10 '21 00:00


use belowed code to get the array of each element's text with same class

 var text =[];
$(".mainmenu").each(function(){
text[text.length]=$(this).text();
});
console.log(text)
dilip

dilip
answered Jun 10 '21 00:00

By map function , it works like this

  
$(".mainmenu").map(function(){
return $(this).text();
}).get().join(",");
 
Post Answer