/    Sign up×
Community /Pin to ProfileBookmark

Conflict with two scripts on one page

I have two scripts I need to use in one page. One script I received from these forums and another I found online.

They both depend on what is selected from an option select menu from a form. One hides/displays content related to the selection at a different part of the screen, and the other changes the available options of other option selections.

They both work great independently, but when combined and placed in the head, only the control over other option selections remains.

I’m guessing there is some conflict, but I haven’t been able to figure out exactly what it is, and how to fix it.

Thank you so much for any help.

[code=html]<script type=”text/javascript”>
function showSelection () {
var e, i = 0;
while (e = document.getElementById (‘sel1’).options [i++]) {
document.getElementById (e.value).className = ‘hide’
}
document.getElementById (document.getElementById (‘sel1’).options [document.getElementById (‘sel1’).selectedIndex].value).className = ”;
}

if (document.getElementById) onload = function () {
showSelection ();
document.getElementById (‘sel1’).onchange = showSelection;
}
</script>
<script type=”text/javascript”>
function dynamicSelect(id1, id2) {
if (document.getElementById && document.getElementsByTagName) {
var sel1 = document.getElementById(id1);
var sel2 = document.getElementById(id2);
var clone = sel2.cloneNode(true);
var clonedOptions = clone.getElementsByTagName(“option”);
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
sel1.onchange = function() {
refreshDynamicSelectOptions(sel1, sel2, clonedOptions);
};
}
}
function refreshDynamicSelectOptions(sel1, sel2, clonedOptions) {
while (sel2.options.length) {
sel2.remove(0);
}
var pattern1 = /( |^)(select)( |$)/;
var pattern2 = new RegExp(“( |^)(” + sel1.options[sel1.selectedIndex].value + “)( |$)”);
for (var i = 0; i < clonedOptions.length; i++) {
if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
sel2.appendChild(clonedOptions[i].cloneNode(true));
}
}
}
</script>
<script type=”text/javascript”>
window.onload = function() {
dynamicSelect(“sel1”, “sel2”);
dynamicSelect(“sel2”, “sel3”);
dynamicSelect(“sel3”, “sel4”);
}
</script>[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@KorJul 19.2005 — this is the wellknown [b]onload[/b] conflict. The rule is that there must not be more than one [b]onload[/b] event on the same document (window). To solve the conflict you must group the function triggered by onload within the same [b]onload[/b] handled function:

So:

  • 1. Remove the part:



  • --------
    if (document.getElementById) onload = function () {

    showSelection ();

    document.getElementById ('sel1').onchange = showSelection;

    }
    -------



    and insert the desired codelines in the other handler:

    window.onload = function() {

    dynamicSelect("sel1", "sel2");

    dynamicSelect("sel2", "sel3");

    dynamicSelect("sel3", "sel4");

    [color=red]showSelection ();

    document.getElementById ('sel1').onchange = showSelection;[/color]


    }
    Copy linkTweet thisAlerts:
    @JeremyHauthorJul 19.2005 — Thank you very much Kor for your help. Would you recommend that I would then also combine the scripts to one so they are all inside one script tag?
    ×

    Success!

    Help @JeremyH 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.18,
    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,
    )...