I couldn't figure out to do it unless I added some form of ID to each
input field.  I'm not sure how one can get the place of an element
within a jQuery object, so this is how I approached your problem.

 

Tested in Win/FF2

 

<form>

            <input type="checkbox" id="1"><label>box 1</label><br />   

            <input type="checkbox" id="2"><label>box 1 and 2</label><br
/>   

            <input type="checkbox" id="3"><label>box 1 and 2 and
3</label><br /> 

            <input type="checkbox" id="4"><label>box 1 and 2 and 3 and
4</label><br />

</form>

 

<script>

 

$(document).ready(

            function () {

                        $("[EMAIL PROTECTED]").each(

                                    function () {

                                                $(this).bind(

                                                            "click",

                                                            function ()
{

 
$(this).siblings("[EMAIL PROTECTED]").lt($(this).id()-1).each(

 
function () {

 
$(this).attr(

 
{

 
checked: 'true'

 
}

 
);

 
}

 
)

                                                            }

                                                )

                                    }

                        )

            }

);

 

</script>

 

The code can be cleaned up a bit, but I like making it verbose while
trying to figure things out so that's what you get.

 

If someone knows how to snag the place of an element within a jQuery
collection so that number can be passed to the lt() function I'd love to
hear it.

 

-ALEX

 

________________________________

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Glen Lipka
Sent: Monday, November 27, 2006 4:27 PM
To: jQuery Discussion.
Subject: [jQuery] Checkbox: Selecting all Previous

 

This might be a newbie question, but I am having trouble with it.

 

I have 4 checkboxes vertical

 

<input type="checkbox"><label>box 1</label><br />   

<input type="checkbox"><label>box 1 and 2</label><br />   

<input type="checkbox"><label>box 1 and 2 and 3</label><br /> 

<input type="checkbox"><label>box 1 and 2 and 3 and 4</label><br />

 

When I click one, I want to check the boxes of all the ones previous to
it (including it).  And uncheck all the boxes after it.

 

How would I structure the statement in jQuery to select?  I am trying to
avoid the FOR loop.

 

This gives me the last one.

var lastOne = $("#myForm [EMAIL PROTECTED]:checked:last");

 

Not sure how to use this.  I tried prev() but that didnt seem to work.

 

Thanks for the help,


Glen

 

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to