Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  169] [ 1]  / answers: 1 / hits: 19514  / 13 Years ago, mon, july 11, 2011, 12:00:00

I need to get the text that is inside a element.



I can only grab the class of this element and NOT the ID.



<span class=fileName>test.png</span>


So I need a way to get test.png, but as you see I have only the class of the element and not the ID.



Just notice also that we may have more <span class=fileName></span>, so it could look like this



<span class=fileName>test1.png</span>
<span class=fileName>test2.png</span>
<span class=fileName>test3.png</span>
<span class=fileName>test4.png</span>


In the case we have more, like the example above, I need to get ALL the values and not only one, because I need to pass this value to another page with jQuery. So it should be able to get one value or more from that element.



Please help!



And also I am not a javascript expert!


More From » jquery

 Answers
19
var filenames = $('.fileName').map(function(){
return $(this).text();
}).get();


The array filenames will contain all the names of the images. You can pass on this array to another jQuery function, or anywhere else you like to do so.



You can test it here »



Update



Since you request the filenames to be a string separated by a comma, you can do it like this:



var filenames = $('.fileName').map(function(){
return $(this).text();
}).get().join(',');


Now, filenames will contain the string test1.png,test2.png,test3.png,test4.png.


[#91249] Saturday, July 9, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaitlyn

Total Points: 421
Total Questions: 73
Total Answers: 100

Location: South Georgia
Member since Sat, Jul 25, 2020
4 Years ago
;