Definition : Document.getElementById

Asked

Viewed 11,695 times

4

Someone can give me a good explanation about Document.getElementById ... I am reading/learning JSON and I have some questions related to the function !

  • 1

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

  • OK I have noticed that it is a JV function and not a JSON function ... That is to say what is the function of Document.getElementById ?

  • Yes... I have noticed that JSON server as Point of Communication with a Server through Manipulated Javascript... Something Like That

2 answers

9


About the getElementById

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

See the MDN definition:

https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById

To illustrate, if you have

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

And in JS makes a

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

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

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

http://codepen.io/bacco/pen/LxGXjv


About the JSON

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

What is JSON? What is it for and how it works?

  • In a way, it is more appropriate to use the getElementById than the querySelector or varies from case to case??

  • getElementByID returns A specific item, querySelector is already a kind of filter that returns the first item that meets the criteria passed. Each has its function, usually not interchangeable. The second has to scan the DOM to locate what is being sought.

4

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

Function document.getElementById()

Returns the element reference via its ID.

That is, it serves to "grab" an element of the GIFT (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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.