Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Loop through checkboxes, verify those present & skip those not?

    Hi all,

    I couldn't figure out how to describe this question in the title. Sorry for any confusion there might be.

    I have a form that has checkboxes next to images. If checked, the image will be deleted upon form submit. There are always 10 images possible, however, some images will not always be present. If they are not present (the image), then a repsective delete checkbox will not be shown.

    The problem I'm running into is that this js will fail for undefined when it hits a "missing" checkbox.

    the relevent HTML
    Code:
    <form name="photos" onsubmit="return verify_photos()">
    <input type="checkbox" name="deletePhoto1">
    <input type="checkbox" name="deletePhoto2">
    <input type="checkbox" name="deletePhoto3">
    <input type="checkbox" name="deletePhoto4">
    <input type="checkbox" name="deletePhoto5">
    <input type="checkbox" name="deletePhoto6">
    <input type="checkbox" name="deletePhoto7">
    <input type="checkbox" name="deletePhoto8">
    <input type="checkbox" name="deletePhoto9">
    <input type="checkbox" name="deletePhoto10">
    </form>
    The JS
    Code:
    function verify_photos() {
      for (h=1;h<=10;h++) {
        if (photos["deletePhoto" + h].checked) { //**** Fails here if a cooresponding  photo[h] checkbox isn't present. ****//
          var answer=confirm("Are you sure you would like to delete the checked photo(s)?");  
          if (answer) {
    
          } else {
            for (i=1;i<=10;i++) {
     	  if (photos["deletePhoto" + i].checked) {
                photos["deletePhoto" + i].checked = false;
     	  }
            }
            return false;
          } 
        } 
        return true;
      } 
    }
    How can I skip over undefined (i.e. not there) checkboxes?

    Thanks!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    if (photos["deletePhoto" + h] && photos["deletePhoto" + h].checked)
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Quote Originally Posted by felgall View Post
    if (photos["deletePhoto" + h] && photos["deletePhoto" + h].checked)
    Thank you felgall, for the reply. Much appreciated.

    I couldn't get
    photos["deletePhoto" + h] &&
    to work, but I wound up getting
    photos["deletePhoto" + h] != null &&
    to work.

    Thanks again!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •