Problem with CSS not applying in DIV

Asked

Viewed 111 times

0

People wanted to know why the id attribute of the div is not being recognized by the CSS, and the id name of the DIV is right in the CSS.

  function nome () {
    var tela = document.createElement('div').setAttribute('id', 'screen')
    document.body.appendChild(tela)
  }

  nome()

1 answer

2

Your code should be:

  function nome () {
    var tela = document.createElement('div')
    tela.setAttribute('id', 'screen')
    document.body.appendChild(tela)
  }

  nome()

This is because the value of the screen variable does not have a correct type (expected Htmldivelement) so that it can be injected into the GIFT. In other words, the method setAttribute() returns "void". In fact, CSS is not applying to your DIV element because it doesn’t exist, because of the wrong instruction. When you do:

var tela = document.createElement("div").setAttribute("id","screen"); 

You actually create the DIV, but it’s still not in the DOM. And here you try to inject it into the DOM, but by the fact of the variable tela have a value "nothing" (Undefined), this DIV is never placed in your HTML document:

document.body.appendChild(tela); // essa linha não faz sentido
  • What do you mean ? I didn’t know that.

  • I must have written something off the chart on this var?

  • I’ll see that, thanks for the help felipsmartins

  • @Danielsantos, look at the issue that explains why.

  • I was able to solve it, it was a very simple error,.

  • was a wrong turn

  • so it said that the variable did not have a nodename

  • he had to load the page body first, and not the external script because then the copier would not even know where to place the div,and so in fact the variable would have no value at all,.

  • took long but I found

  • thanks friend for the message about the possible error,helped me a lot I did a research on this node and found out why

Show 5 more comments

Browser other questions tagged

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