3

I am trying to check a series of buttons to see if they have been selected in order to generate a query string. So far as I can tell, the logic looks something like this:

if ($("input[name='ovr']")[0].checked)
{
...
}

This works, but since I don't want to use a series of if statements to generate the string (because more buttons might be added later, it's inefficient, etc), I generated an array representing the names associated with the buttons (I printed the array; it definitely contains the proper names). When I made a loop to run through each name in the array, however, the console suggested that 'input' was an unrecognized expression. Here is the loop:

    for (i = 0; i < myList.length; i = i + 1) {
        if ($("/"input[name='" + myList[i] + "']/"")[0].checked) {
            string += myList[i] + "=" + myList[i] + "&";
        }
    }

string is a variable that appends the proper signature(?) onto itself if the button check should return true. myList is the array containing the names.

Unfortunately, I am new to all of this and don't really know where to begin. I did try to google the answer (really a novice here) but I wasn't even quite sure what to search for. I also messed around with the phrasing of the input expression, used different escape characters, etc., to no avail. Neither the HTML nor most of the javascript is mine (it's my supervisor's), so I am doubly lost. Any suggestions you could give would be sincerely appreciated. Thanks!

1
  • 2
    Welcome to Stack Overflow! The more effort you put into making your question clear and readable, the more and better answers you'll get. When you were writing your question, to the right there was a handy How to Format box. Worth a read, as are the various bits of information available from the [?] link above the text area. There's also a handy preview area under the question as you write it, so you can see how it will appear. (If all else fails, there's also an "edit" link under the question after you post it.) Jonathan was kind enough to clean it up for you this time. Commented Jul 6, 2012 at 15:16

2 Answers 2

2

Something like this would work (I don't really understand why you tried to add those backslashes, so I only show you the right way):

for (i = 0; i < myList.length; i = i + 1) {
    if ($("input[name='" + myList[i] + "']")[0].checked) {
        string += myList[i] + "=" + myList[i] + "&";
    }
}

One note: if you are generating a query string, don't forget to use encodeURIComponent()!

Sign up to request clarification or add additional context in comments.

2 Comments

You know, I must be stupid or something because I could've sworn I tried that implementation yesterday and it didn't work. Tried it just now: it works. That's slightly embarrassing. Anyway, thanks for your time! And we are using encodeURIComponent(). I was messing with the backslashes for reasons related to me thinking the parameter had to have quotation marks around it and me wanting to use the " escape character, though I see now that I messed that up too.
@user1507116 No problem at all, we all make strange mistakes sometimes. If my answer helped, you can accept it with the tickmark next to the answer.
1

something like this?

var query = "?";
$('input:checked').each(function(index) {
    query += this.name + "=" + this.value + "&";
});

you can modify the selector to get only the checkboxes you need.

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.