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

192
Visualizações
Edit specific input button based on user input, and save it with a separate button in vanilla JS

I'm trying to make an input, it has its value from the beginning.
Then, when the user double click on a span, the cursor needs to be in input field and the user should be able to edit it.
Once edit is ready, he needs to press save button in order to save it, not just click outside of the input field(which is by default)

The second button will just delete, reset the name string, and then once again when you press save, it will save the empty string.

All that, will depend on which span the user is dblckicking, so the respective input of a respective span has to be accessed for edit.

I've done something like this for now..
But it's far to be ready. Would appreciate you're ideas.
I need to do it using vanilla JavaScript

var numberOfbuttons = document.querySelectorAll(".name").length;

for (i = 0; i < numberOfbuttons; ++i) {
     
      document.querySelectorAll('.name')[i].ondblclick = function() {
      document.getElementById('in1').disabled = false;    
      document.getElementById('in1').value = 'hahmmm';
      };
      
}
<span id="spn1" class="name">
   <input id="in1" type="text" disabled="true" value="Homer Simpson">
   <div class="namefunc">
      <button class="check">save</button>
      <button class="close">delete</button>
   </div>
</span>
<span id="spn2" class="name">
   <input id="in2" type="text" disabled="true" value="Marge Simpson">
   <div class="namefunc">
      <button class="check">save</button>
      <button class="close">delete</button>
   </div>
</span>
<span id="spn3" class="name">
   <input id="in3" type="text" disabled="true" value="Bart Simpson">
   <div class="namefunc">
      <button class="check">save</button>
      <button class="close">delete</button>
   </div>
</span>

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

0

In order to make the correct input field editable, you must not work with the ID of the first input field in the loop. Here is a way to select the correct input field via the double-clicked span element:

document.querySelectorAll(".name").forEach(span => {
    span.ondblclick = function() {
        let input = span.querySelector("input");
        input.disabled = false;
        input.value = "hahmmm";
        input.focus();
        span.querySelector("button.check").onclick = function() {
            input.disabled = true;
        }
        span.querySelector("button.close").onclick = function() {
            if (input.disabled == false) {
                input.value = "";
                input.focus();
            }
        }
    }
});

window.onclick = function() {
    let active_input = document.querySelector("input:not([disabled])");
    if (active_input) {
        active_input.focus();
    }
}

I'm not quite sure what the save button is supposed to do. Right now it just disables the input field again, keeping the new value.

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