I have 10 divs
<div id="div_1" class="myDivs"></div>
<div id="div_2" class="myDivs"></div>
<div id="div_3" class="myDivs"></div>
...
O want to select 5 of them with a click handler using jQuery.
$(".myDivs").on("click", function() {
console.log('all clicked DIVs IDs...');
}
Is there a functionality to do this with jQuery? I would like to click them and get all IDs of the clicked divs
. Thanks for your help!
I have 10 divs
<div id="div_1" class="myDivs"></div>
<div id="div_2" class="myDivs"></div>
<div id="div_3" class="myDivs"></div>
...
O want to select 5 of them with a click handler using jQuery.
$(".myDivs").on("click", function() {
console.log('all clicked DIVs IDs...');
}
Is there a functionality to do this with jQuery? I would like to click them and get all IDs of the clicked divs
. Thanks for your help!
2 Answers
Reset to default 4This does the trick:
$(".markDIV").on("click", function (evt) {
if (evt.ctrlKey)
$(this).toggleClass("marked");
});
Toggle a class on each clicked div, then get an array of the ids of the divs with the class. The clicking of CTRL is a little redundant when using div
elements. Try this:
$(".myDivs").on("click", function() {
$(this).toggleClass('selected');
var selectedIds = $('.selected').map(function() {
return this.id;
}).get();
console.log(selectedIds);
});
Example fiddle
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744993188a4605030.html
评论列表(0条)