Click running wrong

0

I have this code:

function loadEditLabel() {
  // Salva o novo input saindo do campo ou apertando enter
  $('[contenteditable="true"]').focus().select().keydown(function(event) {
      if (event.key == 'Enter') { // Checa se a tecla digitada foi o Enter
        $(this).prev('input').val(this.innerHTML); // Colocar o value do input com o texto digitado
        $(this).prop('contenteditable', false); // Desabilita o campo de edição
      }
    })
    .blur(function() {
      $(this).prev('input').val(this.innerHTML);
      $(this).prop('contenteditable', false);
    });
}

$('#add').click(function() {
  html = '<div>';
  html += '<input type="radio" style="vertical-align: text-bottom;" value="teste" name="data">';
  html += '<input type="text" placeholder="Nova Entrada">';
  html += '</div>';

  $('#radios').append(html); // Adiciona o novo input dentro da div radios

  loadEditLabel(); // Carrega o radio para a edição
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="form-group esconder" id="id_2">
  <div class="panel panel-default">
    <div class="panel-body">
      <div id="multipla-escolha">
        <label class="" for="orderBy">Pergunta</label>
        <input class="form-control " type="text" placeholder="Pergunta">
        <div class="container">
          <div id="radios">
            <div>
              <input type="radio" style="vertical-align: text-bottom;" value="teste" name="data"><input type="text" value="Opção 1">
            </div>
          </div>
          <button id="add">+</button>
        </div>
      </div>
    </div>
  </div>
</div>

It's meant to add radios , but I should only add it when I clicked on button , but any click I add, even on other pages, dialog .

    
asked by anonymous 12.06.2017 / 21:57

1 answer

1

The following is the possible answer to your problem.

When you create the $('#add').click event you are telling jQuery that every time there is a click in any element type in your document that contains the "add" ID, it executes that code snippet.

Probably your other pages should contain some parent element with the add id, in which case javascript would simply interpret your code.

To resolve, try modifying the button id for something you have not used on your page. Example: "btnAddPaginaTal", and modify both your id button and jquery code.

  

Note: I have tested your code and with me the problem did not occur, so I believe it is a parent element with the "add" id that is causing this problem.

See JSFiddler with your code working.

    
12.06.2017 / 22:23