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

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..

Related

How can I do jquery's parent() in JavaScript? [duplicate]

This question already has answers here:
Getting the parent div of element
(7 answers)
Add CSS attributes to element with JavaScript
(5 answers)
Set CSS property in JavaScript?
(11 answers)
Closed 3 months ago.
I'm trying to translate this jQuery into JavaScript, to save a bit on load time. Here's the line of jQuery:
$('div.annotated p > span.citation').parent().css('display', 'inline')
And here's what I have so far in JavaScript:
document.querySelectorAll('div.annotated p > span.citation').forEach((el) => {})
I'm stuck on how to find the parent of an element, and also apparently how to set the CSS for that element. Should I stick with jQuery, or can JavaScript also do this, and is it much harder?

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

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.

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.

Add single attribute without value using jquery [duplicate]

This question already has answers here:
Set attribute without value
(7 answers)
Closed 4 years ago.
I want to add an attribute on a div, something like this:
<div class='media' data-type-sticky></div>
Using jQuery, so i've tried this:
jQuery('.media').attr(data-type-sticky);
jQuery('.media').attr('data-type-sticky');
Just pass empty string to attr value.
$('.media').attr('data-type-sticky','');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class='media'>Hello</div>

How to get <input> default value in this code? [duplicate]

This question already has answers here:
How to pick element inside iframe using document.getElementById
(6 answers)
Closed 7 years ago.
In my javascript code, I want to retrieve the value of a TAG placed in page_1 (which id is: "token"), i've tried the following but it does not work:
<iframe id="Myiframe" src="http://page_1">
<script>
doc = document.getElementById('Myiframe');
value = doc.getElementById("token").value;
</script>
Any idea?
document.getElementById("myText").defaultValue

Categories