Delete input and leave only the first one

1

I have a question about removing inputs using js , for example I have 7 inputs and I want to delete 6 of them leaving only the first, try to use remove() but this eliminates all of them. thanks in advance.

$(document).ready(function(){
    $('#unica-pregunta').change(function(){
        var id=$(this).attr("id");
        var opciones=id.match(/^([a-zA-Z]+)\-([0-9]+)/g)[0]+'-opcion';
        if ($(this).is(':checked')) {
            $('#'+opciones).attr('disabled',true).remove().val('');
        }else {
            $('#'+opciones).attr('disabled',false).show();
        }
    });
});
    
asked by Miguel Angel 19.09.2018 в 22:00
source

1 answer

1

You can try this:

$('#eliminar').click((e) => {
  e.preventDefault();
  $("#formulario").find(".form-group").not(':first').remove();

});

$('#ocultar').click((e) => {
  e.preventDefault();
  $("#formulario").find(".form-group:first").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">


<form id="formulario">
  <div class="form-group">
    <label>Input1</label>
    <input type="input" class="form-control" id="input1" placeholder="input1">
  </div>
  <div class="form-group">
    <label>Input2</label>
    <input type="input" class="form-control" id="input2" placeholder="input2">
  </div>
  <div class="form-group">
    <label>Input3</label>
    <input type="input" class="form-control" id="input3" placeholder="input2">
  </div>
  <button class="btn btn-primary" id="eliminar">Eliminar</button>
  <button class="btn btn-primary" id="ocultar">Ocultar el Primero</button>
</form>

If you notice create a form with 3 input, you can place the amount you want and in the js if you notice I invoke the #formulario and then I say to find everyone who has this class .form-group and with the not (': first') I'm telling you "except the first one" , I hope you serve

    
answered by 19.09.2018 / 22:25
source