How to get the value of the value and also the name of the checkbox at the same time as selecting jquery

Question:

I need to get the name of the selected checkbox as soon as I click on one of the checkboxes on the page. I already got the value , but I also need to get the name value.

Could someone help me or give me another alternative because I need to get the value of name and value at the same time as clicking.

$(document).ready(function () {
		
	$('input[name="1117"]').click(function () { 
		selecionado('1117'); 
	}); 
	var selecionado = function (grupo) { 
		var result = $('input[name="' + grupo + '"]:checked'); 
		if (result.length > 0) { 
			var contador = result.length + " selecionado(s)<br/>"; 
			result.each(function () { 
				contador += $(this).val() + " "
			}); 
			$('#divFiltros').html(contador); 
		} 
		else { 
			$('#divFiltros').html("Nenhum selecionado"); 
		} 
	}; 
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="divFiltros"></div>

<form method="POST"> 
      
      <legend>Marca</legend>
      
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6327">
           <label for="">Samsung</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6328">
           <label for="">Motorola</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6329">
           <label for="">Sony</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="6327" name="1117" value="6330">
           <label for="">LG</label>
      </div>
      
      <legend>Especificação</legend>
      
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="450" name="1120" value="450">
           <label for="450">4G</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="455" name="1120" value="455">
           <label for="455">2 Chips</label>
      </div>
      <div class="checkbox checkbox-success">
           <input class="styled marca" type="checkbox" id="461" name="1120" value="461">
           <label for="461">Video 4K</label>
      </div>
</form>

Answer:

The name is a property of the object. It is also an attribute of the element that this object represents.

That means you can get that value with .name or with getAttribute('name'); .

So if what you're looking for is to know which items were selected, you can use:

var escolhidos = $('input:checked');

to know which checkbox checked, and then map this array with just what matters: the name and the value .

var escolhidos = $('input:checked');
var selecionados = escolhidos.map(function() {
     var produto = {nome: this.name, value: this.value};
     return produto;
}).get(); // o .get() é para discartar o jquery e trabalhar só com uma array nativa

example: https://jsfiddle.net/3nexkLnn/

Scroll to Top
AllEscort