I need to build a string variable using jQuery to find all the text nodes surrounded by span tags. Importantly I need to separate each segment of text with the pipe character "|".
For example take the following html:
<div id="myDiv">
<span>this</span> <span>is</span> <span>the</span> <span>text</span>
</div>
Using jQuery I need to produce the following:
var my_variable = "this|is|the|text";
So far I have the following jQuery statement but this just adds the pipe character at the end and doesn't separate each segment of text.
var my_variable = $("#myDiv").find("span").text() + '|'
;
Any help would be very much appreciated.
Note: To remove the last pipe character at the end of the string I was going to use:
my_new_variable = "my_variable.slice(0, -1)";