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

213
Visualizações
How can I assign unique ID to innerhtml element?

I have this function:

function displayInputIntro() {
                var submitbutton = document.getElementById('submitbutton')
                var intro = document.getElementById('Intro') 
                var introNewSection = document.getElementById('intro-row')
                introNewSection.innerHTML += '<div class="col-lg-5 mb-lg-0 mb-4" id="intro">';
                submitbutton.style.display = ''
}

This function displayInputIntro() can be called multiple times, and it will insert this new section into the body of the HTML as many times as one decides to do so. Problem is, I want to add a unique identifier, so that if I were to add something like this:

introNewSection.innerHTML += '<div class="col-lg-5 mb-lg-0 mb-4" id="intro"><a href="javascript;;" onclick="deleteInputFunc()">Remove Input</a>';, 

and click on Remove Input, then it would delete the input, not all of them.

I have tried something like this:

var uniqueID = new Date()
introNewSection.innerHTML += '<div class="col-lg-5 mb-lg-0 mb-4" id="' + uniqueID '"'><a href="javascript;;"  onclick="deleteInputFunc()">Remove Input</a>';, and click on `Remove Input`, then it would delete the input, not all of them.

but that doesn't work. How can I do this?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

you are passing an string to html code, not the real JS variable. You can try something like this thant I found in How can I apply a variable as an element ID with JavaScript?:

 var id = new Date();

 var html ='<li id="' + id + '">'
           + '<input type="image" id="' + id + '" src="images/subtract.png" onclick="javascript:deleteProduct(this.id)" href="#">'
           + id
       + '</li>';
about 4 years ago · Santiago Gelvez 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