Raymond
Raymond

Reputation: 137

jquery map return values and attribute value

i have checkboxes and i want to get the value and attribute value of checked box.

<input type="checkbox" class="checkedtransfer nomargin" id="'.$r->id.'" enrollno="'.$r->enrollno.'" />  

now in jquery i want to store the values and attribute value of each checked checkboxes in one array.

so far this works for me, i did two map function which return the correct values, but i want it in one array only. can i simplify my existing code?

var id = checkboxes.map(function(){ return $(this).attr("id"); }).get();
var enrollno = checkboxes.map(function(){ return $(this).attr("enrollno"); }).get();

Upvotes: 0

Views: 826

Answers (1)

guradio
guradio

Reputation: 15555

Add an obj then add the values there then return the obj

var arr = $(":checkbox").map(function() {
  var obj = {};
  obj['id'] = $(this).attr("id");
  obj['enrollno'] = $(this).attr("data-enrollno");
  return obj;
}).get();

console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="checkedtransfer nomargin" id="1" data-enrollno="1" />

Upvotes: 1

Related Questions