Why document.getElementById() command doesn't work on my code? [duplicate] - javascript

This question already has answers here:
How to get value of a div using javascript
(7 answers)
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 1 year ago.
I'm trying to learn innerHTML commands, but it's just showing me "Author: " text.
HTML side
<h1 id="author">Author: </h1>
<p>Sabir Türkleri'nin ve Batı Göktürk boylarının devamı olan Hazarlar, Göktürk birliği döneminde Göktürklerin Batı kanadını oluşturmaktaydı.</p>
</body>
JS side
var content= "Ensar Ergok";
document.getElementById("author").value=content;

You need to use innerText, textContent or innerHTML instead of value.

Related

Javascript Select HTML ID element directly without "getElementbyID" [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
Why don't we just use element IDs as identifiers in JavaScript?
(5 answers)
Is there a spec that the id of elements should be made global variable?
(5 answers)
Automatically created variables from ids in JS? [duplicate]
(6 answers)
Why is document.getElementById not needed? [duplicate]
(1 answer)
Closed 2 years ago.
I just noticed, its not necessary anymore in Chrome to select elements that have an id e.g with getElementById, you can directly call that element with its id name... e.g "para1" in this example:
<head>
<title>getElementById Beispiel</title>
<script>
function changeColor(newColor) {
para1.style.color = newColor;
}
</script>
</head>
<body>
<p id="para1">Irgendein Text</p>
<button onclick="changeColor('blue');">Blau</button>
<button onclick="changeColor('red');">Rot</button>
</body>
is there any more specification on this? didn't find anything about it on the internet..

id value as variable vs document.getElementById [duplicate]

This question already has answers here:
Do DOM tree elements with IDs become global properties?
(5 answers)
document.getElementById("someId") Vs. someId
(5 answers)
Closed 4 years ago.
I have a snippet of code here
<div id="pTagId" >some content</div>
<button onclick="console.log(document.getElementById('pTagId').innerHTML);">button1</button>
<button onclick="console.log(pTagId.innerHTML);">button2</button>
In the first button I'm using document.getElementById to get the tag object.
In the second button I'm directly using pTagId without document.getElementById and it also works.
is it reliable to use pTagId directly without document.getElementById? If yes what is the need of document.getElementById?
getElementById() is the right way to do it.
There are some browsers that make a global variable by the same name as the element id so that may be why it was somehow working, but you should not rely on that.

How do I change the text in Javascript? [duplicate]

This question already has answers here:
How to change a text with jQuery
(8 answers)
Closed 6 years ago.
I have a javascript code. The code always appends some text to an old one. But I dont want that the text is attached to the old text. Instead, I want that it replaces the old text.
Here is the code line:
$("#werkstatt").append("Text");
How can I do that?
Thank you very much!
You have to use empty() method.
$("#werkstatt").empty().append("Text");
or simply you should use .text() method.
$("#werkstatt").text("Text");

Raw javascript equivalent to jQuery.empty() [duplicate]

This question already has answers here:
Remove all child elements of a DOM node in JavaScript
(37 answers)
Closed 9 years ago.
I found some jQuery code to clear the contents of a div:
$('#masterdiv').empty();
Is there any similar way to do this in raw javascript, without jQuery or any other library?
This code will work.
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
Mention the id of the tag on myNode for which you want to remove the inner child.
Please have a look on
http://jsfiddle.net/2dJAN/19/
<div class="btn-post">123</div>
$(".btn-post").html("")

How to get HTML element similar to getting BODY with document.body? [duplicate]

This question already has answers here:
How to get the entire document HTML as a string?
(16 answers)
Closed 5 years ago.
I know there's a way to do this, but I cannot recall how.
How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?
Use document.documentElement.
See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement
var html = document.getElementsByTagName('html')[0];

Categories