SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2005
    Location
    London
    Posts
    1,678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why isnt confirm working here??

    Hi,

    I have this simple function that allows me to check if any checkboxes are checked on a page...if any are im asking for them to confirm if they want to delete the items or not. Problem is that currently even if the user selects cancel to stop the deleting the code still runs and the selected items get deleted...

    Any ideas what i need to do to get it working?

    Code JavaScript:
    function checkCheckBoxes() {
      var boxChecked = false;
      var inputs = document.getElementsByTagName("input");
      for(var i=0; i<inputs.length; i++) {
        var input = inputs[i];
        var checkys = input.getAttribute("type");
        if(checkys == 'checkbox') {
          if(input.checked) {
            boxChecked = true;
          }
        }
      }
      if(boxChecked == true) {
      var answer = confirm('Are you sure you want to delete these items?');
      if(!answer) {
          return false;
      }
     
          }
     
     
      else {
        alert("You have not selected any items to delete");
        return false;
      }
    }

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Although it depends on how you actually use this function, my guess is that the problem is that you never return true.

    Try,
    Code JavaScript:
    if (boxChecked) {
      return confirm('Are you sure you want to delete these items?');
    }
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

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