How to make editable radiobutton?

Asked

Viewed 178 times

2

Next, I will have to add radiobuttons dynamically, in that they will have to be able to write. They come as label ne? I needed to be able to edit this label, it could be a text too, the important thing is to be able to add the radiobutton and the user can write whatever you want on the label

inserir a descrição da imagem aqui


Thank you for answering Junior, just a doubt. my html ta thus:

                  <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" value="teste" name="data">
                             </div>
                          </div>
                          <button id="add">+</button>
                       </div>
                    </div>
                 </div>
              </div>
           </div>

and when it renders it already brings 2 radio, and when I click on anything, it adds more radios, knows what can be?

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" value="" name="data">';
  html += '<label contenteditable="true"></label>';
  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 id="radios">
  <div>
    <input type="radio" value="teste" name="data">
    <label>Teste</label>
  </div>
  <div>
    <input type="radio" value="teste2" name="data">
    <label>Teste2</label>
  </div>
</div>

<button id="add">+</button>


I will add a print, because simulating by jsfiddle does not happen the same error. Just so you know, he’s getting all the clicks before he even gets to the screen, and he’s adding the radio.inserir a descrição da imagem aqui

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 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>

  • 1

    Please put some code snippet that you have already done to clarify.

2 answers

2

Do you want to say something along those lines? The label can be a text input?

<h3>Sexo:</h3>
<form action="">
  <input type="radio" style="vertical-align: text-bottom;" name="gender" value="male"> <input type="text" placeholder="Masculino"><br>
  <input type="radio" style="vertical-align: text-bottom;" name="gender" value="female"> <input type="text" placeholder="Feminino"><br>
</form>

Using the JS provided by @Juniornunes you could also do something on that line:

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 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>

  • Hi Leon, it worked your code too!! just fell into the same problem as Junior’s. I’m trying to understand why

  • @Isabellameirelles Can’t simulate your problem here? Any chance you use one of our codes and share the error here?

  • I got it solved!! Just one thing, I added a remove button as: https://jsfiddle.net/71hmxrr8/, only it doesn’t work, in this example that you gave me, it would have to be created differently?

2


You can do so too:

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" value="" name="data">';
  html += '<label contenteditable="true"></label>';
  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 id="radios">
  <div>
    <input type="radio" value="teste" name="data">
    <label>Teste</label>
  </div>
  <div>
    <input type="radio" value="teste2" name="data">
    <label>Teste2</label>
  </div>
</div>

<button id="add">+</button>

NOTE: Tested in Chrome Version 59.0

Browser other questions tagged

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