How to insert a line break into an appendchild, is it possible?

Asked

Viewed 154 times

-2

Do I need a line break after an appendchild or would it be after input? I’m a beginner in JS and I’m a little lost on how to make this line break.

var inp1 = document.createElement('input')
            var inp1 = document.createElement('input')



            var text1 = document.createTextNode('Digite o primeiro numero: ')
            var text2 = document.createTextNode('Digite o segundo numero: ')



            div.appendChild(text1)
            div.appendChild(inp1) //quebra de linha no final do input
            div.appendChild(text2)

            var container = document.querySelector('#div')
            container.appendChild(div)

1 answer

1


Come on buddy, the beginning may seem a little confusing, but being careful and paying attention to what you’re developing can work.

First, the code is repeating the creation of the input in the first and second row:

var inp1 = document.createElement('input')
var inp1 = document.createElement('input')

Second, you have the element div not created in the code you shared:

            div.appendChild(text1)
            div.appendChild(inp1) //quebra de linha no final do input
            div.appendChild(text2)

As long as you pay attention to the variable names, always try to make it as readable as possible. Follow the code as it could be:

  var inputFirstNumber = document.createElement('input')

  var labelFirstNumber = document.createTextNode('Digite o primeiro numero: ')
  var labelSecondNumber = document.createTextNode('Digite o segundo numero: ')

  var div1 = document.createElement('div')
  var div2 = document.createElement('div')
  var div3 = document.createElement('div')

  div1.appendChild(labelFirstNumber)
  div2.appendChild(inputFirstNumber)
  div3.appendChild(labelSecondNumber)

  var container = document.querySelector('#container')
  container.appendChild(div1)
  container.appendChild(div2)
  container.appendChild(div3)

Link to Codepen: https://codepen.io/kleberoliveira/pen/YzXoWzj

Good luck!

  • Thank you very much friend, very good to have advice like this at the beginning! I will follow your advice! I created the 3 div elements and gave right, thank you very much!

  • @Janioalbuquerque Note that the solution to your problem is not even related to JS, it is a matter of HTML. CSS could also be used if necessary.

Browser other questions tagged

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