Definition: document.getElementById

4

asked by anonymous 11.01.2017 / 10:49

2 answers

9


About getElementById

Basically document.getElementById , as the name already says, is a JavaScript function that serves to return a DOM element that is identified by a specific ID.

See the MDN definition:

  

link

To illustrate, if you have

<div id="aaa">Hoje</div>
<div id="bbb">Amanha</div>
<div id="ccc">Ontem</div>

And in JS do a

var teste = document.getElementById('bbb');

is recovering the second teste of the HTML above in the variable div .

Here's a very simple example of how to change the value of the div of the example:

  

link


About JSON

Right here on the site we have a question with what you are looking for:

  

What is JSON? What is it good for and how does it work?

    
11.01.2017 / 11:01
4

Note that JSON has nothing to do with Javascript with regard to its purpose, JSON is just like XML, it is to be read / manipulated by software, only a format that allows the communication of information between systems / languages many different. JSON (Javascript Object Notation) only has in common the syntax of an object in JS.

As for document.getElementById()

  

Returns the reference of the element through its ID.

That is, you can "grab" an element of DOM (document, HTML) through your id, ex :

var elemento = document.getElementById('meu_id');
console.log("este é o elemento de que agora (graças a document.getElementById('meu_id')) temos a referência: ", elemento);

console.log('Vamos por ex mudar o seu comprimento/altura/cor de fundo');

elemento.style.width = "100px";
elemento.style.height = "120px";
elemento.style.backgroundColor = "blue";

console.log('Agora o elemento tem ' +elemento.style.width+ ' de comprimento, tem ' +elemento.style.height+ ' de altura e tem ' +elemento.style.backgroundColor+ ' como cor de fundo');
<div id="meu_id"></div>
    
11.01.2017 / 11:05