1
var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">&#160;</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>. &#160;</div>';           
var str = "";
$(markup).find("div[class^='ExternalClass']").each(function(){
    str += $(this).text();
})

How do I grab content of all the divs in the markup that starts with ExternalClass?

5
  • Do you want to parse HTML or a javascript string? Because you say you want to parse the string, but your jQuery selector looks like you're parsing HTML nodes. Commented Oct 10, 2016 at 15:44
  • @user2415266 Updated. parse the variable, please Commented Oct 10, 2016 at 15:45
  • But all divs in variable start with ExternalClass. Commented Oct 10, 2016 at 15:52
  • Sorry just realised the class isn't 'ExternalClass' but begins with External Class. But then this just makes me wonder why you have so many classes, that all start with ExternalClass and all use really complex names? Commented Oct 10, 2016 at 15:53
  • @DibsyJr I have absolutely no control over the markup- controls emit the markup like that.. Commented Oct 10, 2016 at 16:01

2 Answers 2

2

$(markup) selector contain all ExternalClass class and you can't use .find() because it doen't any matched child. You need to use .filter() to filter selected element.

var markup = "<div...";
var str = "";
$(markup).filter("div[class^='ExternalClass']").each(function(){
    str += $(this).text();
})

var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">&#160;</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>. &#160;</div>';
$(markup).filter("div[class^='ExternalClass']").each(function(){
    console.log($(this).text());
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

1 Comment

That actually would be the correct answer here. Mine is bad in comparison.
1

jQuerys .find() only loops through the children of the specific HTML you selected. Your variable markup has no children with the fitting class selector. The easiest way I can imagine getting this solved, is to wrap all you have in markup inside another div, and then use your jQuery selector - that works:

var markup = '<div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">Employee Self-Service pages have been corrected but may require you to refresh the page.</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">&#160;</div><div class="ExternalClass34E9F553C2F74AA2B6D693A07BA166AC">If the problem remains, follow <a href="/Shared%20Documents/EBS%20Page%20Refresh%20Instructions_rkc.pdf">these instructions</a>. &#160;</div>';
markup = '<div>' + markup + '</div>';
var str = "";
$(markup).find("div[class^='ExternalClass']").each(function(){
    str += $(this).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.