First line
\n
Second line
\n
Third line
\n
First line
\n
Second line
\n
Third line
\n
First line
\n
Second line
\n
Third line
\n
What is the HTML output of following code?<div id="container"> <p class="p1">First line<p> <p class="p2">Second line<p> <p class="p3">Third line<p></div><script> document.getElementsByClassName('p1').innerHTML = 'Viblo';</script>
Question
What is the HTML output of following code?<div id="container"> <p class="p1">First line<p> <p class="p2">Second line<p> <p class="p3">Third line<p></div><script> document.getElementsByClassName('p1').innerHTML = 'Viblo';</script>
Solution
The HTML output of the code will remain the same as the original HTML code. The JavaScript code is trying to change the innerHTML of the element with class 'p1' to 'Viblo', but it won't work because getElementsByClassName returns a HTMLCollection (which is an array-like object of all child elements), not a single element.
So, the correct JavaScript code should be:
document.getElementsByClassName('p1')[0].innerHTML = 'Viblo';
This will change the first paragraph text to 'Viblo'. But in your original code, no changes will be made. So, the output will be:
<div id="container">
<p class="p1">First line<p>
<p class="p2">Second line<p>
<p class="p3">Third line<p>
</div>
Similar Questions
What is the HTML output of following code?<body><h2>JavaScript HTML DOM</h2><div id="container"> <p id="para-1">This is a paragraph.</p> <p id="para-2">This is another paragraph.</p></div><script> const p = document.createElement("p"); p.innerHTML = 'New line'; const firstP = document.getElementById('para-1'); document.getElementById("container").insertBefore(p, firstP);</script></body>
What is the HTML DOM?
What is the correct JavaScript syntax to change the content of the HTML element below?<p id="demo">This is a demonstration.</p>{$a->questionintifier} Yanıta.document.getElementByName("p").innerHTML = "Hello World!";b.#demo.innerHTML = "Hello World!";c.document.getElement("p").innerHTML = "Hello World!";d.document.getElementById("demo").innerHTML = "Hello World!";
What HTML code will be generated when this component is rendered?
What is the output of the code below<!DOCTYPE html><html><body><h1>HTML5 Canvas</h1><canvas id="myCanvas" width="300" height="150" style="border:1px solid grey"></canvas></body></html>
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.