Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
How do I use JS in html without DOM

I'm very new to programming. Ive used w3schools and freecodecamp to learn. I know the basics within objects, arrays, functions and loops. I know that I can write something like this to change an image.

function a(b) {var t; if(b === 1){t = "image.jpg"} document.getElementById("aide").src= t; } <br>button onclick="a(1)" <br>
img id="aide src="some.image"

How would a professional programmer do this? Is that called dom as i need to write onclick in html?

and.. I can make object/arrays such as var x = [{ rock: "Metallica" }];. I want a type="text" bar on a webpage that displays Metallica when a user writes Rock in the text field and submits it. How, and without dom if possible?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you want to change an image, you can change its source:

// setting the variables used later
var btnNewImage = document.getElementById('btn-new-image')
var imageContainer = document.getElementById('image')
var counter = 1

// creating the string that will
// be the src of the image
function getImgSrc(currentCounter) {
  // the current counter makes it possible to
  // load different images on click
  return "https://picsum.photos/200/300?random=" + currentCounter
}

// setting the src attribute of the imageContainer
// modifying the counter
function setImage() {
  imageContainer.src = getImgSrc(counter)
  // counter is mutated, so if it's called again
  // it will be the next number
  counter++
}

// adding event listener to the button
// instead of onclick handler
btnNewImage.addEventListener('click', function() {
  // calling the setImage function, that will set
  // the src attribute of the image
  setImage()
})

// calling the setImage function for the first time
// so an image shows up on page load
setImage()
<button id="btn-new-image">NEW RANDOM IMAGE</button><br />
<img id="image" src="" />

The snippet above uses some things not really advised:

  • var instead of let & const
  • using that certain variables & functions are in the same scope (mutating counter, calling setImage() function, referencing imageContainer inside the setImage() function, etc.)

These are to be avoided, but I think at this point it makes no difference.

The API I used for images: Lorem Picsum

The trick is that addEventListener does not do anything - until there's a click on the button it's been registered to. When the click happens, it executes the function that's added to it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda