Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2012, 11:23 AM   PM User | #1
Piece Of Meat
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Piece Of Meat is an unknown quantity at this point
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?
Piece Of Meat is offline   Reply With Quote
Old 10-01-2012, 10:25 PM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 10-03-2012, 10:24 AM   PM User | #3
Piece Of Meat
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Piece Of Meat is an unknown quantity at this point
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.
Piece Of Meat is offline   Reply With Quote
Old 10-03-2012, 11:12 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
Piece Of Meat (10-04-2012)
Old 10-03-2012, 11:32 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Piece Of Meat (10-04-2012)
Old 10-04-2012, 12:27 PM   PM User | #6
Piece Of Meat
New Coder

 
Join Date: Oct 2012
Posts: 16
Thanks: 3
Thanked 0 Times in 0 Posts
Piece Of Meat is an unknown quantity at this point
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.
Piece Of Meat is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:27 AM.


Advertisement
Log in to turn off these ads.