0

I want to extract part of html string which format is given as below. This html string is returned by var contents = $("#content").html()

blah1 blah1
<p> blah2 blah2 </p>

Then I use

start = contents.indexOf("<p>");
end = contents.indexOf("</p>");

to get the start and end positions. So I can use contents.slice(start, end) to extract the string I want.

But in IE browser the <p> tag is capital <P> and in Chrome and Firefox, they user <p>

How can I include both cases so that my JavaScript function can work in all browsers? Thanks

1
  • don't use string operations when you're already using jquery... $('#content p').text() will retrieve everything inside the P for you. Commented Nov 21, 2012 at 19:18

2 Answers 2

5

You can use:

$('#content p').text();
Sign up to request clarification or add additional context in comments.

Comments

2

Why not just grab the innerHTML of the p tag in question?

jQuery:

var contents = $("#content > p").html();

or even, just the plain text:

var contents = $("#content > p").text();

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.