Saturday 31 August 2013

How to convert a jquery array to a string array in javascript

How to convert a jquery array to a string array in javascript

I ran the following jquery command:
x=$(".container h3")
and got back this response:
[<h3>&#8203;A</h3>&#8203;, <h3>&#8203;B&#8203;</h3>&#8203;,
<h3>&#8203;C&#8203;</h3>&#8203;, <h3>&#8203;D&#8203;</h3>&#8203;,
<h3>&#8203;E</h3>&#8203;]
I would like to convert each of the elements in the array to a string, so
the final result would look like this:
["A", "B", "C", "D", "E"]
The problem is that I can't convert each jquery response object into a
string element.
I tried to cast each object as a String, via:
for(var i=0; i < x.length; ++i) {console.log( String(x[i]) )}
But I got back this:
[object HTMLHeadingElement]
[object HTMLHeadingElement]
[object HTMLHeadingElement]
[object HTMLHeadingElement]
[object HTMLHeadingElement]
Is there a direct way of converting each object into a string element?

No comments:

Post a Comment