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

276
Visualizações
How can js code be split across multiple files

I want to change the HTML code on the button click. Is there any other way to accomplish this? The issue I have faced is that over time ex1,ex2 codes get bigger and it becomes hard to read due to a text being a string.

<button onclick="test (1)">
click
</button>
<button onclick="test (2)">
click
</button>
<div id="ex1"></div>
const faq = {
ex1: `<button>1</button>`,
ex2: `<button>2</button>`
}

function test (number) {
if (number == 1){
document.getElementById("ex1").innerHTML = (faq.ex1);
}
if (number == 2){
document.getElementById("ex1").innerHTML = (faq.ex2);
}
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You just needed to remove the space between your function invocation.

Instead of this:

test (1)

do this

test(1)

Also you need to remove the space between your function name and the parenthesis

Instead of this

function test ()

Do this

function test()

Also you need to put your javascript into a script tag and make sure it comes before the html that calls it. See example snippet

<script>
const faq = {
ex1: `<button>1</button>`,
ex2: `<button>2</button>`
}

function test(number) {
if (number == 1){
document.getElementById("ex1").innerHTML = (faq.ex1);
}
if (number == 2){
document.getElementById("ex1").innerHTML = (faq.ex2);
}
}
</script>
<button onclick="test(1)">
click
</button>
<button onclick="test(2)">
click
</button>
<div id="ex1"></div>

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