<p>This is probably a really simple jQuery question, but I couldn't answer it after 10 minutes in the documentation so...</p>

<p>I have a list of checkboxes, and I can get them with the selector <code>'input[type=checkbox]'</code>. I want the user to be able to shift-click and select a range of checkboxes. To accomplish this, I need to get the index of a checkbox in the list, so I can pass that index to <code>.slice(start, end)</code>. How do I get the index when the user clicks a box?</p>

[{'answer_id': 43824, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 4, 'selected': True, 'text': '<p>The following selector should also work in jQuery: <code>input:checkbox</code>.</p>\n\n<p>You can then string the <code>:gt(index)</code> and <code>:lt(index)</code> filters together, so if you want the 5th to 7th checkboxes, you\'d use <code>input:checkbox:gt(4):lt(2)</code>.</p>\n\n<p>To get the index of the currently clicked checkbox, just use <code>$("input:checkbox").index($(this))</code>.</p>\n'}, {'answer_id': 43854, 'author': 'Ryan Duffield', 'author_id': 2696, 'author_profile': 'https://Stackoverflow.com/users/2696', 'pm_score': 1, 'selected': False, 'text': '<p>This is a quick solution, but I would give each checkbox a unique ID, perhaps with an index hint, like so:</p>\n\n<pre><code>&lt;input id="checkbox-0" type="checkbox" /&gt;\n&lt;input id="checkbox-1" type="checkbox" /&gt;\n&lt;input id="checkbox-2" type="checkbox" /&gt;\n&lt;input id="checkbox-3" type="checkbox" /&gt;\n&lt;input id="checkbox-4" type="checkbox" /&gt;\n</code></pre>\n\n<p>You can then easily obtain the index:</p>\n\n<pre><code>$(document).ready(function() {\n  $("input:checkbox").click(function() {\n    index = /checkbox-(\\d+)/.exec(this.id)[1];\n    alert(index);\n  });\n});\n</code></pre>\n'}, {'answer_id': 43898, 'author': 'Christian Oudard', 'author_id': 3757, 'author_profile': 'https://Stackoverflow.com/users/3757', 'pm_score': 0, 'selected': False, 'text': "<p>Thanks for the answer, samjudson.</p>\n\n<p>After further experimentation, I found that you can even use just <code>$(':checkbox')</code> to select them. It's interesting that you can use the <code>.slice()</code> function to get the range, but you also have the option of doing it in the selector with <code>:gt</code> and <code>:lt</code>. I do find the syntax of <code>.slice()</code> to be cleaner than using the selector filters, though.</p>\n\n<p>I'm going to have to say that I don't like Ryan Duffield's solution as much, because it requires changes to the markup, and involves repeating code.</p>\n"}, {'answer_id': 43924, 'author': 'Ryan Duffield', 'author_id': 2696, 'author_profile': 'https://Stackoverflow.com/users/2696', 'pm_score': 0, 'selected': False, 'text': "<p>@Gorgapor: I guess I need to take questions a little less literally sometimes.  :-)  I figured you were locked down to requiring some sort of index.  I think you'll find though that as you use jQuery more, you usually don't need to do that sort of thing.</p>\n"}]
['https://Stackoverflow.com/questions/43811', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3757/']