0

I have a question regarding HTML element selection using javascript.

Here is my code:

Javascript

<script>
    function changeText() { 
        document.getElementsByClassName("CentralContainer")[0].getElementsByClassName("PlayFlowSelect")[0].childNodes[1].innerHTML = "Custom Stream";
                }
</script>

HTML

<div class="CentralContainer">
        <div class = "PlayFlowSelect">
            <input type="image" src="./Home Page Resources/ArrowLeftIcon.png" onclick="changeText()">
            <h2> Top Stories </h2>
            <input type="image" src="./Home Page Resources/ArrowRightIcon.png" onclick="alert('Trigger Upload Page')">
        </div>

For some reason my selection path in my javascript isn't selecting the h2 innerHTML. I'm not sure why... Any help?

4
  • 3
    Probably you're selecting a text node (space between elements). Try to get the nodeType of the node retrieved Commented Jan 21, 2015 at 15:40
  • I don't think it is a good idea to select an element this way. If for some reason you put some other html element before the <h2> tag, your code will not work properly. Something like @atmd's answer looks better to me. Commented Jan 21, 2015 at 15:47
  • 2
    @Al.G. I agree - this is likely to break in the long term. JLRishe's answer explains well why it isn't currently working though, and is the correct answer in this instance. Using querySelectorAll would be better still, as you could just use document.querySelectorAll('.CentralContainer .PlayFlowSelect h2') Commented Jan 21, 2015 at 15:54
  • @JamesThorpe OK, I agree. JLRishe explains the real reason how it works, but if I had to do it, I would do it atmd's way. Commented Jan 21, 2015 at 16:00

3 Answers 3

2

As suggested in the comments, the childNodes property includes all child nodes, including text nodes, which would mean that the h2 you want would be childNodes[3].

You can use the .children property to get just element children:

function changeText() { 
    document.getElementsByClassName("CentralContainer")[0]
            .getElementsByClassName("PlayFlowSelect")[0]
            .children[1].textContent = "Custom Stream";
}

But as other answers and comments have suggested, this is not the cleanest or most reliable way to do this.

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

2 Comments

What is the difference between textContent and innerHTML?
The difference is that el.textContent = '<b>Hi!</b>'; would display <b>Hi</b> in the page, while el.innerHTML = '<b>Hi!</b>'; would display Hi! in the page. The best practice is to avoid innerHTML unless you specifically treat a value as HTML. innerHTML creates a risk for XSS vulnerabilities and other problems.
1

You could change it slightly to get the H2 element by the tag name

document.getElementsByClassName("CentralContainer")[0].
getElementsByClassName("PlayFlowSelect")[0].
getElementsByTagName('h2')[0].innerHTML;

Reads a bit cleaner (IMO) then more array node indexes and avoid additional id's

Comments

-1

Give the <h2> tag an ID and call getElementById()

function changeText() { 
        document.getElementById("header").innerHTML = "test";
}

<div class="CentralContainer">
        <div class = "PlayFlowSelect">
            <input type="image" src="./Home Page Resources/ArrowLeftIcon.png" onclick="changeText()">
            <h2 id="header"> Top Stories </h2>
            <input type="image" src="./Home Page Resources/ArrowRightIcon.png" onclick="alert('Trigger Upload Page')">
        </div>
</div>

Hope this helped. Best regards

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.