A
A
absoluteST2020-10-27 17:17:51
JavaScript
absoluteST, 2020-10-27 17:17:51

2 scripts not working together?

Good evening, there are two scripts, they work fine separately, but together the script for outputting to a paragraph does not work, please help, I can’t fix it myself, the forms should work together on one 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;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pashenka, 2020-10-27
@like-a-boss

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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question