javascriptgoogle-chrome-extensionsplitnextsibling

Performing split on node returned by nextSibling returns TypeError undefined is not a function


I am trying to grab a piece of text next to an element that I can select with its class and then perform a split(" ") on it, but I get a TypeError: undefined is not a function. I think this is because it is not a string, so when I try to convert it to a string first it returns "[object Text]"

html:

<span class="status_abbr_inactive">
<figure class="planetIcon planet tooltip js_hideTipOnMobile" title="Planet"></figure>Hinata [1:28:10]</span>

When I perform

document.getElementsByClassName("planetIcon planet tooltip js_hideTipOnMobile")[0].nextSibling

it returns "Hinata [1:28:10]", but performing split() on it does not work. What am I missing here?


Solution

  • Actually, what you get with that line of code is a textNode.

    To use the text contained in the textNode you can use its .textContent property:

    var text = document.getElementsByClassName("planetIcon planet tooltip js_hideTipOnMobile")[0].nextSibling.textContent;
    
    var splitted = text.split(' ');
    // ["Hinata", "[1:28:10]"]