0

I have a function that checks the page for a given string (date) like so:

function findDate(date) {
    if ((document.documentElement.innerText).indexOf(date) > -1) {
        return this;
    } else {
        return false;
    }
}

My question is, once I find this text, how do I get the parent element so I can later prepend text immediately after the element ends.

For example, if my date is stored in an element like this:

<div id="date">October 6th, 2015</div>

I want to findDate("October 6th, 2015").parentElement.after.prepend("text").

HTML should end up being:

<div id="date">October 6th, 2015</div>stuff

How would I go about doing this?

  • `innerText` is just a string (or nothing in FF), it hasn't any elements. You've to rethink the whole logic, iterate through all the elements on a page to find the text, or maybe use `Selection` and `Range` objects. – Teemu Nov 06 '15 at 19:53
  • Thanks, Teemu. got it. –  Nov 06 '15 at 19:56
  • 1
    I'd look at something like the TreeWalker [here](http://stackoverflow.com/a/2579869/1324). You'll test each text node for the target text; if there's a match, then `myTextNode.parentNode` will get the `div` in question; `myTextNode.parentNode.nextElementSibling` would be the following element. – Paul Roub Nov 06 '15 at 19:59

0 Answers0