Sandra Willford
Sandra Willford

Reputation: 3789

remove everything (elements and text) after a given child element

I am working in our Gmail extension right now, and given that the I have the parent Element and it looks something like below, I need to remove everything after <br clear="all" />, given that what is after may or may not exist given the users Gmail signature settings. Thoughts?

<div
    id=":93"
    class="Am Al editable LW-avf"
    hidefocus="true"
    aria-label="Message Body"
    g_editable="true"
    role="textbox"
    aria-multiline="true"
    contenteditable="true"
    tabindex="1"
    style="direction: ltr; min-height: 416px;">
    Test
    <br clear="all" />
    <div>
        <br />
    </div>
    -- <br />
    <div dir="ltr" class="m_-1358982132021700848m_4914548661371847332gmail_signature" data-smartmail="gmail_signature">
        <div dir="ltr">
            <b>Suzy Smith</b>
            <div>
                <i><a href="mailto:[email protected]" target="_blank">[email protected]</a></i>
            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 95

Answers (1)

epascarello
epascarello

Reputation: 207501

Just loop over the nextSibling and remove it.

var elem = document.querySelector('[clear="all"]')
while(elem.nextSibling) {
  elem.nextSibling.remove()
}
elem.remove()
<div
    id=":93"
    class="Am Al editable LW-avf"
    hidefocus="true"
    aria-label="Message Body"
    g_editable="true"
    role="textbox"
    aria-multiline="true"
    contenteditable="true"
    tabindex="1"
    style="direction: ltr; min-height: 416px;">
    Test
    <br clear="all" />
    <div>
        <br />
    </div>
    -- <br />
    <div dir="ltr" class="m_-1358982132021700848m_4914548661371847332gmail_signature" data-smartmail="gmail_signature">
        <div dir="ltr">
            <b>Suzy Smith</b>
            <div>
                <i><a href="mailto:[email protected]" target="_blank">[email protected]</a></i>
            </div>
        </div>
    </div>
</div>

Upvotes: 3

Related Questions