Explain Line of Code (You can loop through Object properties by using the for-in loop)

Asked

Viewed 47 times

0

document.getElementById("demo").innerHTML += myObj[x] + "<br>";
  • Is concatenating the value of myObj[x] the content of the id element demo.

  • I was wrong, I am already going to restructure the previous Question ... I’m sorry, but I was clarified with your Answer !

  • Already this ! Does not Spoil, because the Answer is already below the Correct Question

  • 1

    @Maniacsaw do so, when opening a question, look for the code as complete as possible, but also explain the parts you understand and the ones you don’t, so we can focus the explanation on the part you’re really struggling with.

  • 1

    ok... I’ll do it in the next few questions

  • And don’t be afraid to ask, the recommendation is only for a better use of the site. Over time, get the hang of.

  • ... What does the title have to do with the question?

Show 3 more comments

1 answer

5


As you saw in the previous question,

 document.getElementById("demo")

to recover from DOM (the page structure, mounted according to HTML) an element defined by id="demo".

Then the

.innerHTML

retrieves a reference to the HTML inside this element. Then,

+= 

means "add at end" a certain value ( a += b amounts to a = a + b in virtually any language).

What will be added to the element is this:

myObj[x] + "<br>";

Whereas myObj[x] is a array, i.e., a collection of items. In this case, the added item will be taken from the variable x.

  • Thank you very much ... And I’m sorry for these somewhat "amateur" questions, but I’m trying to understand my doubts 100% to have a good foundation !

  • 1

    I am already reading ... I thought I was Creating but I was Editing ... To Mutch Coffe .... Excuse me .... I will try to be as clear as possible in the future .

  • @Bacco I know your answer has already been accepted, but looking at the issue history of the question, there was more important content there. I suppose the OP has chosen to remove the pertinent HTML by not knowing how to format it, which causes the SE engine to delete it.

  • @Victorstafusa he ended up asking a separate question. He even reversed pq I answered the previous version. His intention was good, but in the end this extra content ended up staying in another better post, so I returned to the state that was..

Browser other questions tagged

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