Get input name values with Jquery

1

I have an application with several checkboxes, and this code with Jquery :

var checkboxes = $(".borrar_contacto");
console.log(checkboxes);

And this with JavaScript :

var checkboxes2 = document.getElementsByClassName("borrar_contacto");
console.log(checkboxes2);

In console.log they do not show me the same results. With Jquery he shows me the following:

[input.borrar_contacto, input.borrar_contacto, 
input.borrar_contacto, input.borrar_contacto, prevObject: jQuery.fn.init[1]]

And with JavaScript the following:

[input.borrar_contacto, input.borrar_contacto, 
input.borrar_contacto, input.borrar_contacto, 
10: undefined, 11: undefined, 12: undefined, 13: undefined]

With JavaScript, it shows me the values of the input name in the list, but with Jquery no. How can I get him to show me exactly the same with Jquery?

    
asked by fed R 23.01.2017 в 06:04
source

4 answers

1
  

How can I get him to show me exactly the same with Jquery?

So that you can get exactly the same with jQuery , you could do the following:

  • Convert HTMLCollection to array , using the method .slice()
  • Convert jQuery object to array , using the .toArray() method

So for example:

var checkboxes2 = document.getElementsByClassName("borrar_contacto");
// Convertimos el HTMLCollection a array
console.log([].slice.call(checkboxes2));

var checkboxes = $(".borrar_contacto");
// Convertimos el jQuery object a array
console.log(checkboxes.toArray());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label><input type="checkbox" class="borrar_contacto" value="1" />Contacto 1</label>
<label><input type="checkbox" class="borrar_contacto" value="2" />Contacto 2</label>
<label><input type="checkbox" class="borrar_contacto" value="3" />Contacto 3</label>
<label><input type="checkbox" class="borrar_contacto" value="4" />Contacto 4</label>
    
answered by 23.01.2017 / 14:32
source
2

Get the value of the 'name' attribute of the inputs:

$('.borrar_contacto').attr('name');

Get the value that the input has entered (parameter value):

$('.borrar_contacto').val();

Greetings.

    
answered by 23.01.2017 в 10:54
2

If your inputs have an assigned class called "delete_contact" you can use:

$('.borrar_contacto').each(function(){
         console.log($(this).attr("name"));
});

Greetings

    
answered by 23.01.2017 в 14:01
0

To get the values of the input name with Jquery you can use the following piece of jquery code:

$("input[name='borrar_contacto']").each(function() {
    console.log($(this).val());
});

With this code you get all the values of inputs with the name borrar_contacto .

    
answered by 23.01.2017 в 11:01