0

When I use jquery find() to get elements, it will return a collection of objects.

So, basically I run:

 $("#eventlist").find("span")

Now if I run:

 $("#eventlist").find("span").html()

It returns the html of just the first element in the object.

When I try

 JSON.stringify($("#eventlist").find("span"))

I get an error: DOMException: Failed to read the 'selectionDirection' property from 'HTMLInputElement':

My question is, how could I get all elements in one big html string?

2
  • you want html of all in one place? Commented Dec 9, 2014 at 4:52
  • yes, just a big html string containing all "<span>content</span><span>content2</span><span>content3</span>" Commented Dec 9, 2014 at 4:53

3 Answers 3

1

You can use .map() like

var array = $("#eventlist").find("span").map(function () {
    return this.outerHTML
}).get();
var html = array.join('')
Sign up to request clarification or add additional context in comments.

Comments

1

You can alterate over each and can stroe html in some varaible, see the ouptut and see console

var html="";
$("#eventlist").find("span").each(function(){

html+=$(this).parent().html();
})

console.log(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="eventlist">
<span>content1</span>
<span>content1</span>
<span>content1</span>
<span>content1</span>
  </div>

Comments

0

use outerHTML in javascript

$("#eventlist").find("span").each(function () {
   console.log( this.outerHTML);
})

Comments

Your Answer

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.