I want to have the following in my javascript code using jquery:
$('.attrs span:nth-child(1)').text() + ' | ' + $('.attrs span:nth-
child(2)').text() + ' | ' + $('.attrs span:nth-child(3)').text() ...
Though the number of children span elements will change so I have worked on it dynamically like so:
function getString(containerElem) {
var str = '';
for (var i = 0; i < containerElem.children().length; i++) {
((i + 1) == containerElem.children().length) ? str += '$(\'.attrs span:nth-child(' + (i + 1) + ')\').text()' : str += '$(\'.attrs span:nth-child(' + (i + 1) + ')\').text() + \' \' + ';
}
return str;
}
When I return it as a string the jQuery will not work as the $ is a string now as well, can I do this using some flag or global variable to add to the string somehow ie $.getString() + $.getString2()... depending on how many span elements my container has?
$('.attrs span:nth-child(1)').text()is returned as a string, You could use eval(Yourstring), but if I was you, i would look for a better way of doing this.