2

I have a following question from my last post.

Jquery html() issue

I want to store each node as full html markup.

The data array should be something like

['<em>test</em>', 'here for the testing purpose',
'second line', '<strong>texts</strong>', 'here']

The codes I have now:

if(this.nodeType === 3) { // Will only select element nodes
    data.push($(this).text());
}else if(this.nodeType === 1){
    data.push($(this).html());
}

but it only store

['test', 'here for the testing purpose','texts','here'] 

Is it possible to store the html markup as well?

Thanks so much!

1
  • array in js are [...] not (...) Commented Jul 19, 2013 at 22:15

2 Answers 2

3

Replace

data.push($(this).html());

with

data.push(this.outerHTML);

Remember

this -- DOM object

$(this) -- jQuery Object

Try using DOM objects instead of jQuery objects whenever possible as the former are a bit faster, because it eliminates an extra overhead of converting them to latter and then apply a method.. Should not be a big deal but just for info.

Sign up to request clarification or add additional context in comments.

Comments

2

element.outerHTML returns the markup including the outer tags of the element:

data.push(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.