Paul Paul - 1 month ago 13
jQuery Question

can I concat html dom elements

I would like to do something like

$(".form-horizontal").find("h3.tableName").html().join(',')


(I have my database tables in h3 dom)

and get a string output:

user,family,photo


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

Answer

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();
}).get();

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

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