0

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();
1
  • 1
    Note in my answer I changed enrollno to data-enrollno because enrollno is not a valid attribute. Commented Mar 15, 2018 at 3:29

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.