Paul Paul - 7 months ago 58
jQuery Question

can I concat html dom elements

I would like to do something like


(I have my database tables in h3 dom)

and get a string output:


Can I to it with a single line code or Im forced to do it with an array ?


Your current logic doesn't work as jQuery returns an object from a selector, not an array. To solve this you can use map() to retrieve data from a set of elements and place it in an array, like this:

var arr = $('.form-horizontal h3.tableName').map(function() {
    return $(this).text();

If you want to retrieve a comma delimited string, you can then join() this array:

var str = arr.join(',');