Click working wrong

Asked

Viewed 46 times

0

I have that 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>

<div class="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 serves to add radios, but only that it should add only when clicked on button, only any click that I give, including on other pages, add radio to this modal dialog.

  • 1

    If you run the code presented in the question, you will see that the new element is only added by clicking the button +, then have as [Edit] the question and better detail the problem?

  • I put an Alert in the js function, and it gives Alert in any clicks I give from the modal fade, IE, this click ta is adding the radios. I don’t know why, but this click that should be exclusive to the add button, is running when I click any type

1 answer

1


Below is the possible answer to your problem.

When creating the event $('#add').click you’re telling jQuery that every time there’s a click in any type of element in your document that contains the "add" ID it performs this code snippet.

Probably, on your other pages should contain some parent element with id add, and in that case javascript would simply be interpreting your code.

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

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

See on Jsfiddler with your code working.

  • 1

    Wow, that was it. Thank you!!!

  • Please mark the answer as correct :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.