1

I ran the following jquery command:

x=$(".container h3")

and got back this response:

[<h3>​A</h3>​, <h3>​B​</h3>​, <h3>​C​</h3>​, <h3>​D​</h3>​, <h3>​E</h3>​]

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?

asked Aug 31, 2013 at 16:55

2 Answers 2

7

Why not using .map() method?

var x = $(".container h3").map(function(){
 return $(this).text();
}).get(); // ["A", "B", "C", "D", "E"]
answered Aug 31, 2013 at 16:57
Sign up to request clarification or add additional context in comments.

3 Comments

note: if IE support is not required, return this.textContent will do
note that iteration is not the problem. The stringification was.
Thanks for the fast response time :)
1
var x = [];
$(".container h3").each(function(){
 x.push($(this).text());
});
answered Aug 31, 2013 at 17:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.