mercredi 4 février 2015

jQuery Click on an outside element to move unordered list items up or down that contain checked checkboxes

I need a "Move Up" button and a "Move Down" button that will move list items (that contain checkboxes that are checked) up in the list by 1 place or down by 1 place depending on which button they click. I've seen several posts that deal with similar situations but none of them seem to cover exactly what I need. I would like to stick with the primary jquery.js file.



<ul id="theList">
<li>
<label>
<span><input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_0" />Checkbox 1</span></label>
</li>
<li>
<label>
<span><input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_1" />Checkbox 2</span></label>
</li>
<li>
<label>
<span><input type="checkbox" name="CheckboxGroup1" value="checkbox" id="CheckboxGroup1_2" />Checkbox 3</span></label>
</li>
</ul>
<a href="#" id="moveUp">Move Up</a>
<a href="#" id="moveDown">Move Down</a>


jQuery Code:



$('#moveUp').click(function() {
return !$('#theList li :checked').closest('li').insertBefore(this.prev());
});

$('#moveDown').click(function() {
return !$('#theList li :checked').closest('li').insertAfter(this.next());
});




Aucun commentaire:

Enregistrer un commentaire