<!DOCTYPE html>
<html>
<body>
<p>An unordered list:</p>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<p>Click the button to display the innerHTML of the second li element (index 1).</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByTagName("LI");
document.getElementById("demo").innerHTML = x[0].outerHTML;
}
</script>
</body>
</html>
I thought , in first example I should get output: <p><li>Coffee</li></p>, shouldnât I? Many thanks!!!
EDIT
This post has been reformatted by enclosing the code block in 3 backticks
```
on their own lines.
The first li element in the list is Coffee, and the second li element is Tea. That is why you get Tea as the second li element.
The change youâve made to the code from the original results in the zeroth element being retrieved from the array. In JavaScript (and most other languages), arrays are indexed starting from zero.
mkjs still hasnât learned how to format code blocks.
After Discourse stripped out the HTML tags what was displayed looked to be asking for more than it was.
Hey, problem is that I get outputted innerHTML in both cases, in the case I write innerHTML and outerHTMLâŚI supposed in the first code I would get outerHTML as output, but I get innterHTML (just like inthe second example, but there I do expect it, in first code not!)
I have 2 pastebin codefirst with inner and second with outerâŚ
outerHTML is useful when you have an actual element that contains the content.
In this particular case though, there is no outer HTML element because document.getElementsByTagName results in an array instead.
Sidenote: The others have been talking about how you havenât been formatting your code. You need to place three backticks (```) before and after your code, so that it it is handled properly by the forum.
For example:
Code with no backticks gets wrongly interpreted by the forum, as follows:
An unordered list:
Coffee
Tea
Milk
When the code correctly has the three backticks before and after it, it gets formatted properly and itâs easier for people to understand what youâre talking about.
''''
<!DOCTYPE html><html><body> <p>An unordered list:</p><ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li></ul> <p>Click the button to display the innerHTML of the second li element (index 1).</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[0].innerHTML;}</script> </body></html>
<!DOCTYPE html><html><body> <p>An unordered list:</p><ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li></ul> <p>Click the button to display the innerHTML of the second li element (index 1).</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[0].innerHTML;}</script> </body></html>
''''
<!DOCTYPE html><html><body> <p>An unordered list:</p><ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li></ul> <p>Click the button to display the innerHTML of the second li element (index 1).</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[0].innerHTML;}</script> </body></html>
'''
<!DOCTYPE html><html><body> <p>An unordered list:</p><ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li></ul> <p>Click the button to display the innerHTML of the second li element (index 1).</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[0].innerHTML;}</script> </body></html>
<!DOCTYPE html><html><body> <p>An unordered list:</p><ul> <li>Coffee</li> <li>Tea</li> <li>Milk</li></ul> <p>Click the button to display the innerHTML of the second li element (index 1).</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script>function myFunction() { var x = document.getElementsByTagName("LI"); document.getElementById("demo").innerHTML = x[0].innerHTML;}</script> </body></html>