0

Looping over a bunch of checkboxes SOMETIMES fails. The .each function is NOT entered when some or all boxes are checked so I get the "no checkboxes were checked" alert even though "checked" boxes exist.

$(document).ready(function() {
  $("#submitme").click(function() {
    var urls = [];
    $("#edit :checked").each(function() {
      var obj = {};
      obj.url = $(this).val();
      urls.push(obj);
    });
    if (0 < urls.length) {
      $.post('/myurl', {urls: JSON.stringify(urls)});
    }
    else {
      alert("no checkboxes were checked!");
    }
   });
});

<div id="edit">
  <input type="checkbox" name="first"  value="first_url"/>
  <input type="checkbox" name="second" value="second_url"/>
  <button id="submitme">submit</button>
</div>

jsFiddle Example

6
  • one minute ill double check this in a fiddle Commented Jun 23, 2011 at 18:20
  • Do you get errors when this happens, or can you reproduce the problem? Threw this into jsFiddle and it doesn't seem to have failed once... Commented Jun 23, 2011 at 18:20
  • your code looks ok, and does work! see my fiddle: jsfiddle.net/heu96 do you have other code on the page that might be interfering? is this an exact copy of your code or did you retype it? edit: ninja'd i swear neal and bjorn's comments werent there while i started typing this Commented Jun 23, 2011 at 18:22
  • It is funny this works in fiddle b/c when you pull it to an actual page it doesn't parse. The if is messed up. Commented Jun 23, 2011 at 18:32
  • Having updated the syntax with JSLint not complaining anymore the problem still persists. Commented Jun 23, 2011 at 22:08

4 Answers 4

1

No answer to the problem. The issue only exists in Opera so it's browser-related.

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

Comments

0

Seems to be working fine for me: http://jsfiddle.net/maniator/BFuaZ/

Make sure there are no javascript errors anywhere else in your code.

Comments

0

Your selector should be $("#edit input:checked")

Comments

0

I think you have some syntax problems. Try:

$(document).ready(function() {
  $("#submitme").click(function() {
    var urls = new Array();
    $("#edit :checked").each(function() {
      var obj = new Object();
      obj.url = $(this).val();
      urls.push(obj);
    });

    if (0 < urls.length){
      $.post('/myurl', {urls: JSON.stringify(urls)});
    }else {
      alert("no checkboxes were checked!");
   }
   });
   });

If you look in jsfiddle at the JSLint output you get this

Error:

Problem at line 2 character 25: Use the array literal notation [].

var urls = new Array();

Problem at line 4 character 21: Use the object literal notation {}.

var obj = new Object();

Problem at line 9 character 7: Expected '{' and instead saw 'console'.

console.log(urls)

Problem at line 9 character 24: Missing semicolon.

console.log(urls)

Problem at line 11 character 7: Expected '{' and instead saw 'alert'.

alert("no checkboxes were checked!");

Implied global: $ 1,3,5, console 9, alert 11

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.