RussellHarrower
RussellHarrower

Reputation: 6790

Get checkbox's ID

I need to get the ID of checkboxes in a table in the <tbody> section. I have a live(click) function that I want to bring up a popup with the data that i want based in the id in the checkbox.

I want to also allow the system, to have multiple checkboxes checked I want to be able to store the ids in a temp var and the use them as needed and get rid of them as I go though them.

Upvotes: 3

Views: 16758

Answers (2)

jfriend00
jfriend00

Reputation: 707158

$("#container input:checkbox") will be a jQuery object of all the checkboxes inside a particular div with an id of container. You can store that value and use it in the future to carry out all sorts of jQuery operations.

If you want to collect all the DOM objects into a separate array, you can just do a .get() on it.

var domObjects = $("#container input:checkbox").get();

If you want to get all the ids into an array, you could do it this way:

var idArray = [];
$("#container input:checkbox").each(function() {
    idArray.push(this.id);
});

Upvotes: 1

Igor Dymov
Igor Dymov

Reputation: 16460

Just get id with .attr:

$("#container input:checkbox").attr("id")

To get an array of checkboxes ids you might use .map function:

var idArr = $("#container input:checkbox").map(function(i, el) { return $(el).attr("id"); }).get();

Upvotes: 8

Related Questions