Linus Juhlin
Linus Juhlin

Reputation: 1273

Selecting checkboxes

I am having some issues with this project that I am working on.

I have a list of different checkboxes which are records pulled from a database using PHP.

Now, when I click on one of these checkboxes I want that option to be displayed somewhere else somehow. I've been trying to get it to work with jQuery, but I'm not that experienced with it, so there's not really that much I can do.

Upvotes: 1

Views: 154

Answers (3)

Linus Juhlin
Linus Juhlin

Reputation: 1273

I fixed the issue I was having.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(function(){
    $('.checkbox').change(function(event){
        checked_value = $(this).val();          
        if(this.checked)
        {
                            $("#content").append("<div id='" + checked_value +"'>New value: " + checked_value + "</div>");

        }

        if( !this.checked )
        {
            $("#" + checked_value).remove(); 
        }

    });

            $('#checkall').change(function(){
                    $('.checkbox').attr('checked',$(this).attr('checked'))
            });

});
</script>   
    </head>
    <body>
    <input type="checkbox" id="cb1" class="checkbox" value='a' />
    <input type="checkbox" id="cb2" class="checkbox" value='b' />
    <input type="checkbox" id="cb3" class="checkbox" value='c' />

<div id="content"></div>
</body>
</html>

The above code is what I am now using.

I do not remember where I got it from but credits to it's respective owner(s).

Upvotes: 0

ray
ray

Reputation: 457

Keep a hidden variable to store the values of checked checkboxes.

As suggested by "cipher" mention the onclick event of the checkbox to a javascript function. Pass the checkbox object as argument.

In the function check if the checkbox is checked or not checked. If checked, then add the value of the checkbox to the hidden variable. If it is not checked, then remove the value from the hidden variable, incase it was checked before.

This way you always have the list of checked checkboxes.

Upvotes: 0

cipher
cipher

Reputation: 2484

It is actually done by using AJAX. But if you know/want to know jQuery, you should know about AJAX in jQuery. In jQuery you have to call a function associated with click event of the particular id in checkbox. For Simplicity, let us consider each unique Id is given to each og the checkboxes then you can individually call a jQuery Click event to handle that.

<script>
$(function(){

$("#yourid").click(function() {

//do what you want to do here if checkbox "yourid" is clicked
});

$("#yournextid").click(function(){

//do what your next event is.

});
});
</script>

Here 'yourid' , 'yournextid' are id's of respective checkboxes

Upvotes: 2

Related Questions