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 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Ignoring my eventlisteners

    This is the relevant code in the js file:
    Code:
    function  validateForm()
    {
    	 attachEventListener(document.getElementById('addform'), "submit", validateadd, false);
    	 function validateadd()
    	 {
    		 return checking('addfirst','addlast','addmail','addnumber');
    	 }
    	 attachEventListener(document.getElementById('editform'), "submit", validateedit, false);
    	 function validateedit()
    	 {
    		 alert("checking");
    		 return checking('editfirst','editlast','editmail','editnumber');
    	 }
    	 attachEventListener(document.getElementById('addeditform'), "submit", validateaddedit, false);
    	 function validateaddedit()
    	 {
    		 return checking('addeditfirst','addeditlast','addeditmail','addeditnumber');
    	 }
    };
    addform is from index.php, editform and addeditform are from edit.php. The problem is that it ignoring my 2 last attachEventListeners. I put the alert("checking");
    to see if its going into the evenListener and its not. Im connecting to the js file from both index.php and edit.php but its going only into the first eventlistener.
    When i click on the submit buttons of editform and addeditform its not doing the validation. What could be the problam?

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Where are you defining attachEventListener?

    The standard way to attach event listeners is to use addEventListener but IE8 and earlier use attachEvent instead.
    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
    Oct 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    Where are you defining attachEventListener?

    The standard way to attach event listeners is to use addEventListener but IE8 and earlier use attachEvent instead.
    Here is my attachEventListener:
    Code:
    function attachEventListener(target, eventType, functionRef, capture)
    {
      if (typeof target.addEventListener != "undefined")
      {
        target.addEventListener(eventType, functionRef, capture);
      }
      else 
      {
        target.attachEvent("on" + eventType, functionRef);
      }
      return true; 
    };
    I define it on the js file.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    I would suspect that all three of them are running but that whichever call to checking() runs last is the only result that you actually get to see.

    Also there isn't much point in returning anything from checking() as none of your event listeners do anything with that value.
    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.

  • Users who have thanked felgall for this post:

    Piece Of Meat (10-04-2012)

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,588
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ummm...as I read that, you are trying to attach the event listeners to those various forms, WHETHER OR NOT the form exists in that page!

    So the first one that doesn't exist (which will indeed addform if you are on the edit page?) you get an error and everything stops.

    Maybe a simple answer is to ignore any request to set the listener on a null item?

    Code:
    function attachEventListener(target, eventType, functionRef, capture)
    {
      if ( target == null ) { return true; }
      if ( target.addEventListener != null )
      {
        target.addEventListener(eventType, functionRef, capture);
      }
      else 
      {
        target.attachEvent("on" + eventType, functionRef);
      }
      return true; 
    };
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Piece Of Meat (10-04-2012)

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Ummm...as I read that, you are trying to attach the event listeners to those various forms, WHETHER OR NOT the form exists in that page!

    So the first one that doesn't exist (which will indeed addform if you are on the edit page?) you get an error and everything stops.

    Maybe a simple answer is to ignore any request to set the listener on a null item?

    Code:
    function attachEventListener(target, eventType, functionRef, capture)
    {
      if ( target == null ) { return true; }
      if ( target.addEventListener != null )
      {
        target.addEventListener(eventType, functionRef, capture);
      }
      else 
      {
        target.attachEvent("on" + eventType, functionRef);
      }
      return true; 
    };
    I didn't try it because i allready mixed edit and index to be the same page and it solved the problam, but i guess you got a point in what you are saying.
    Another question to: How can i do that everytime there is a mouseover event in a table, the row which i mouseover on changes its color?

    felgall, Thank for your answear but the first eventlistener is working and not the last one so i guess it's not the problem.


  •  

    Posting Permissions

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