0

All of my code is working properly, I'm just trying to expand it to work for all possibilities, know there must be a better way to do it, but don't think I know javascript well enough (I just started learning).

Essentially, I'm trying to call data from a database based on which checkboxes on my web app are checked. There are four boxes: "committees", "candidates", "individuals", "opinion".

My code is this:

if($('#committees').attr('checked')) {
    $.get("file.pl",
    {
       act: "near",
       format: "raw"
       what: "committees"
    }, NewData);
}
else {
    $.get("file.pl",
    {
       act: "near",
       format: "raw"
       what: ""
    }, NewData);

which works perfectly for just the committees checkbox, (the ids of the other boxes match the table name for the "what", and match the above-stated names. I want to have this work for all 4 checked boxes, without having to make huge nested statements for every possible combination of checked boxes.

I know I can list more options in the "what" category like this:

$.get("file.pl",
        {
           act: "near",
           format: "raw"
           what: "committees, candidates, individuals, opinions"
        }, NewData);

and that works, but Idk how to dynamically change that string to match which boxes are checked. Thanks for any help you can offer!

0

3 Answers 3

1

Even though you said that your code works, I would advise that you use .is(':checked') instead of .attr('checked'):

Then you would have to assign each checkbox a class that they all would share, say .checkbox. And your code would be:

$('.checkbox').each(function() {
    if($(this).is(':checked')) {
        $.get("file.pl",
              {
                  act: "near",
                  format: "raw"
                  what: this.id
              }, NewData);
    } else {
        $.get("file.pl",
              {
                  act: "near",
                  format: "raw"
                  what: ""
              }, NewData);
    }    
});

UPDATE

I now realize that a construct like this does need a closure in order for it to work:

$('.checkbox').each(function() {
    (function( that ) {
        if($(that).is(':checked')) {
            $.get("file.pl",
              {
                  act: "near",
                  format: "raw"
                  what: that.id
              }, NewData);
        } else {
            $.get("file.pl",
              {
                  act: "near",
                  format: "raw"
                  what: ""
              }, NewData);
        }
    })( this );   
});
Sign up to request clarification or add additional context in comments.

2 Comments

This didn't work because only the last selection of data goes through, I figured it out though, posted my answer below. Thanks for responding.
I figured as much. Glad you figure it out. The updated version may be helpful if you'd like to process all the checkoxes at once.
0

Do you mean something like this. Use 'this' keyword.
HTML

<input type="checkbox" class="file-pl" id="committees" />
<input type="checkbox" class="file-pl" id="candidates" />
<input type="checkbox" class="file-pl" id="individuals" />
<input type="checkbox" class="file-pl" id="opinions" />

JS

$(".file-pl").click(function(){
    var what = this.checked ? this.id : "";

    $.get("file.pl", {
    act: "near",
    format: "raw"
    what: what
    }, NewData);
});

Comments

0

The above answers didn't quite work unfortunately. What ended up working was this:

var what string = "";
$('.filters').each(function() {
   if($(this).is(':checked')) {
      what string = whatstring+this.id+", ";
     }});
$.get("file.pl",
    {
       act: "near",
       format: "raw"
       what: whatstring
    }, NewData);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.