jQuery Running a function in a context and adding to a variable
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-05-23T03:49:14Z
Indexed on
2010/05/23
3:50 UTC
Read the original article
Hit count: 212
JavaScript
|jQuery
For this question I'm going to give a simplified example of what I want to do, and the function I want to learn to write.
var collection = '';
<div id='container'>
<span>1</span>
<span>2</span>
<span>3</span>
</div>
What I want to do this, for every span in the div, take its text, add it to the collection.
So ideally it would look like:
$('#container').addtoCollection(collection);
The method would be something like:
$(this).find('span').each(function () {
collection = collection + $(this).text();
}).
Where the first this would equal #container.
So in the end you can do:
console.log(collection);
and get:
123
How do I write the function that does this?
Thanks!
© Stack Overflow or respective owner