2

I'm trying to get the content between 2 strings and do operation on them. For example here you can see there are two $toc variables. I want to get the content between these two and make a table of content based on that. If you see the fiddle link you'll see that it's taking all the h1,h2,h3,h4 tags and making table of content by replacing first $toc but I don't want all the H tags in that table of content. I want just the H tags between these two $toc strings. How can I do that?

What I tried

function createTOC(elements) {

    var tocString = '';

    for (var i = 0, len = elements.length; i < len; i++) {
      tocString += elements[i].outerHTML;
    }
    var $body = $('body');
    var html = $body.html();
    var newHtml = html.replace('$toc', tocString);
    $body.html(newHtml);
}
var $tocElements = $('h1, h2, h3, h4');
createTOC($tocElements);
4
  • I don't understand why you need to delimit your content with 2 different p tags with $toc inside... Just wrap what you need in a container, and take the children headings. Extra div won't break your layout, it doesn't have any default styles. Commented Dec 12, 2013 at 9:02
  • actually the requirement is to make table of content where user writes $toc. That I did. Now the requirement is to make separate table of content for separate part, so in the link you see there are 2 $toc, therefore I need to make two table of content. First $toc will make table of content of data till it finds second $toc, and then the second $toc will make table of content of the data till the end of document. Commented Dec 12, 2013 at 9:15
  • I see... I think the container approach still applies though. Or is it more of a markdown thing? Commented Dec 12, 2013 at 9:17
  • no, I'm not aware of what classes/ids user will use, I just need to find the string ($toc) and replace that with table of contents. Commented Dec 13, 2013 at 18:55

1 Answer 1

1

1) find the elements which is holding the text $toc

var listOfTocs = $('p:contains("$toc")');

2) Now using nextUtil() get all the inbetween those p tags.

var foul = $(listOfTocs[0]).nextUntil(listOfTocs[1]);

3) Now filter those elements with elements (h1, h2, h3, h4)

var goal = foul.filter(elements);

4) To see its values just iterate

$.each(goal, function (i, v) {
    console.log($(v).text());
});

Finally,

function createTOC(elements) {
    var listOfTocs = $('p:contains("$toc")');
    var foul = $(listOfTocs[0]).nextUntil(listOfTocs[1]);
    var goal1 = foul.filter(elements);
    $.each(goal1, function (i, v) {
        console.log($(v).text());
    });

    var goal2 = $(listOfTocs[1]).nextUntil($("body").last()).filter(elements);
    $.each(goal2, function (i, v) {
        console.log($(v).text());
    });
}

Returns

Werkwijze
Heading 2
Heading 3
Heading 4
Kwaliteitsregistraties


Goal
Council of members
MGMT
IT
Scope
Environment
Quality
Wellness

Now you can make other things with it.

Hope you understand.

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

8 Comments

@ChankeyPathak Try my updated answer. Since you only 2 $toc, I have directly used array indexing. otherwise you should use iterate each $toc and find the heading inbetween them
I can't use 'p:contains("$toc") as I am not sure whether customer will write $toc in <p> or <div> or whatever. I just need to look for the text $toc.
@ChankeyPathak ohh. Ok let me try some other approach. But to confirm remaining items are okay right?
Hi tried your solution in fiddle, it didn't work. See here. Also to help you understand the complete problem, see this: stackoverflow.com/questions/20515565/…
|

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.