2 scripts not working together?

  • 0
    Good evening, there are two scripts, separately they work fine, but together the script for output to a paragraph does not work, please help me, I cannot fix it myself, the forms must work together on the same page, here is the code:
    <form>
            <input type="checkbox" onclick="checker()">
            <label for="ddr3200">DDR4-3200</label> <br>
            <input type="checkbox" onclick="checker()">
            <label for="ddr2933">DDR4-2933</label> <br>
            <input type="checkbox" onclick="checker()">
            <label for="ddr2666">DDR4-2666</label>
        </form>

    let form = document.forms[0];
            form.onsubmit = checker;
            function checker() {
                let array = [...form.querySelectorAll(":checked")],
                    s;
                if (array.length)
                    s = "Вы выбрали память: " + array.map(e => e.nextElementSibling.textContent).join(', ');
                else
                    s = "Память не выбрана";
                alert(s);
                return false;
            }

    and 2 script
    <form>
            <input type="checkbox" onclick="chatcheck()" id="elem3">
            <label for="ddr3200">DDR4-3200</label> <br>
            <input type="checkbox" onclick="chatcheck()" id="elem4">
            <label for="ddr2933">DDR4-2933</label> <br>
            <input type="checkbox" onclick="chatcheck()" id="elem5">
            <label for="ddr2666">DDR4-2666</label>
            <p class="abzac2"></p>
        </form>

    let formText = document.forms[0];
            formText.onsubmit = chatcheck;
            let abzac2 = document.querySelector('.abzac2');
            function chatcheck() {
                let array = [...formText.querySelectorAll(":checked")],
                    s;
                if (array.length)
                    s = "Вы выбрали память: " + array.map(e => e.nextElementSibling.textContent).join(', ');
                else
                    s = "Память не выбрана";
                abzac2.innerHTML = s;
                return false;
            }
    JavaScript Anonymous, Nov 19, 2020

  • 2 Answers
  • 0
    In the second script, replace

    let formText = document.forms [0];

    on

    let formText = document.forms [1];
    Isaac Montgomery

  • 0
    You are overwriting the onsubmit event handler. If you need multiple handlers for the same event on the same element, use addEventListener instead of overwriting the .onsubmit = ... handler.
    Felix Koch

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!