...

View Full Version : Ignoring my eventlisteners



Piece Of Meat
10-01-2012, 11:23 AM
This is the relevant code in the js file:


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?

felgall
10-01-2012, 10:25 PM
Where are you defining attachEventListener?

The standard way to attach event listeners is to use addEventListener but IE8 and earlier use attachEvent instead.

Piece Of Meat
10-03-2012, 10:24 AM
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:


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.

felgall
10-03-2012, 11:12 PM
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.

Old Pedant
10-03-2012, 11:32 PM
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?



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;
};

Piece Of Meat
10-04-2012, 12:27 PM
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?



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum