/    Sign up×
Community /Pin to ProfileBookmark

Javascript Event Trapping Conflict

Hello,

Glad to be part of this forum! I am developing an html form at work and found two very useful scripts, one is a javascript library to manage showing/hiding form elements, and another which is an javascript calendar.

However when I use them both on the same page the showing/hiding library (usableforms.js) does not work for elements like the checkbox which use onclick property. It does still work for a select.

It seems to me that the calendar script is trapping all onclick events. I have sifted through the source js file, but I am not familar with javascript enough to alter the script so it only traps click events from the calendar.

Usable Forms link (showing/hiding)
site: [url]http://www.quirksmode.org/dom/usableforms.html[/url]
js file: [url]http://www.quirksmode.org/dom/usableforms.js[/url]

Simple Calendar Widget Link
site: [url]http://www.garrett.nildram.co.uk/calendar/scw.htm[/url]
js file: [url]http://www.garrett.nildram.co.uk/calendar/scw.js[/url]

Here is the section of the calendar widget .js which i think where the problem is, but I could be wrong. I’ve tried commenting out the bottom four lines (which I think are used to close the calendar popout when you click outside of it), but that didn’t work:

[CODE] if (document.addEventListener)
{scwID(‘scw’ ).addEventListener(‘click’,scwCancel,false);
scwID(‘scwHeadLeft’ ).addEventListener(‘click’,scwStopPropagation,false);
scwID(‘scwMonths’ ).addEventListener(‘click’,scwStopPropagation,false);
scwID(‘scwMonths’ ).addEventListener(‘change’,scwStopPropagation,false);
scwID(‘scwYears’ ).addEventListener(‘click’,scwStopPropagation,false);
scwID(‘scwYears’ ).addEventListener(‘change’,scwStopPropagation,false);
scwID(‘scwHeadRight’).addEventListener(‘click’,scwStopPropagation,false);
}
else {scwID(‘scw’ ).attachEvent(‘onclick’,scwCancel);
scwID(‘scwHeadLeft’ ).attachEvent(‘onclick’,scwStopPropagation);
scwID(‘scwMonths’ ).attachEvent(‘onclick’,scwStopPropagation);
scwID(‘scwMonths’ ).attachEvent(‘onchange’,scwStopPropagation);
scwID(‘scwYears’ ).attachEvent(‘onclick’,scwStopPropagation);
scwID(‘scwYears’ ).attachEvent(‘onchange’,scwStopPropagation);
scwID(‘scwHeadRight’).attachEvent(‘onclick’,scwStopPropagation);
}

// ***************************
// End of Calendar structure
// ***************************
// ****************************************
// Start of document level event definition
// ****************************************

if (document.addEventListener)
{document.addEventListener(‘click’,scwHide, false);}
else {document.attachEvent(‘onclick’,scwHide);}
[/CODE]

If anybody has any ideas I would really appreciate it.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@davepelzauthorOct 31.2007 — Below is a section of the other script I am using with the calendar widget that is getting overidden. Maybe this script is the problem?


[CODE]
// usableforms.js
// http://www.quirksmode.org/dom/usableforms.html



function prepareForm()
{
if (!compatible) return;
var marker = document.createElement(containerTag);
marker.style.display = 'none';

var x = document.getElementsByTagName('select');
for (var i=0;i<x.length;i++)
addEvent(x[i],'change',showHideFields)

var x = document.getElementsByTagName(containerTag);
var hiddenFields = new Array;
for (var i=0;i<x.length;i++)
{
if (x[i].getAttribute('rel'))
{
var y = getAllFormFields(x[i]);
x[i].nestedRels = new Array();
for (var j=0;j<y.length;j++)
{
var rel = y[j].getAttribute('rel');
if (!rel || rel == 'none') continue;
x[i].nestedRels.push(rel);
}
if (!x[i].nestedRels.length) x[i].nestedRels = null;
hiddenFields.push(x[i]);
}
}

while (hiddenFields.length)
{
var rel = hiddenFields[0].getAttribute('rel');
if (!hiddenFormFieldsPointers[rel])
hiddenFormFieldsPointers[rel] = new Array();
var relIndex = hiddenFormFieldsPointers[rel].length;
hiddenFormFieldsPointers[rel][relIndex] = hiddenFields[0];
var newMarker = marker.cloneNode(true);
newMarker.id = rel + relIndex;
hiddenFields[0].parentNode.replaceChild(newMarker,hiddenFields[0]);
waitingRoom.appendChild(hiddenFields.shift());
}

setDefaults();

addEvent(document,'click',showHideFields);
}


/** ULTRA-SIMPLE EVENT ADDING **/

function addEvent(obj,type,fn)
{
if (obj.addEventListener)
obj.addEventListener(type,fn,false);
else if (obj.attachEvent)
obj.attachEvent("on"+type,fn);
}
[/CODE]
×

Success!

Help @davepelz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...