Paul Paul - 1 year ago 110
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 ?

Answer Source

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(',');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download