1

i have the following code:

    <input type="text" id="wisselspelers" onkeypress="return (event.charCode == 32|| event.charCode > 64 && 
    event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)">
    <button id="opslaanBtn">Wisselspeler toevoegen</button>
    <p id="opgeslagen"></p>

    <script>
        document.getElementById("opslaanBtn").addEventListener("click", myFunction)
        document.getElementById("opslaanBtn").addEventListener("click", myFunction2)

        var spelers, ab1, abc2, abcd3;
        var spelers = [];

        function myFunction() {
            var input = document.getElementById("wisselspelers").value;
   
            spelers.push(input);

            abc2 = spelers.length;
            ab1 = "<ul>";
            for (abcd3 = 0; abcd3 < abc2; abcd3++) {
                ab1 += "<li>" + spelers[abcd3] + "</li>";
            }
            ab1 += "</ul>";
            document.getElementById("opgeslagen").innerHTML = ab1;
        }

        function myFunction2() {
            document.getElementById("wisselspelers").value = "";
        }

    </script>

This code allows someone to enter a value into the input and add it to the array. I'd like to create another button, which allows a user to randomly select one of the values he added to the (empty) array.

I've tried the following code, but this code shows everything the user added and doesn't randomly select one:

  <button id="gekozenBtn">Kies een random wisselspeler</button>
    <h1 id="gekozen"></h1>

    <script>
        document.getElementById("gekozenBtn").addEventListener("click", gekozenFunction)

        var rand = Math.floor(Math.random() * spelers.length);
        var concat = spelers[rand];
        
        function gekozenFunction() {
            document.getElementById("gekozen").innerHTML = (concat);
        }
    </script>

1 Answer 1

1

Putting all your code in the same script tag works:

    <input type="text" id="wisselspelers" onkeypress="return (event.charCode == 32|| event.charCode > 64 && 
    event.charCode < 91) || (event.charCode > 96 && event.charCode < 123)">
    <button id="opslaanBtn">Wisselspeler toevoegen</button>
    <p id="opgeslagen"></p>
  <button id="gekozenBtn">Kies een random wisselspeler</button>
    <h1 id="gekozen"></h1>
    <script>
        document.getElementById("opslaanBtn").addEventListener("click", myFunction)
        document.getElementById("opslaanBtn").addEventListener("click", myFunction2)
        document.getElementById("gekozenBtn").addEventListener("click", gekozenFunction)

        var spelers, ab1, abc2, abcd3;
        var spelers = [];

        function myFunction() {
            var input = document.getElementById("wisselspelers").value;
   
            spelers.push(input);

            abc2 = spelers.length;
            ab1 = "<ul>";
            for (abcd3 = 0; abcd3 < abc2; abcd3++) {
                ab1 += "<li>" + spelers[abcd3] + "</li>";
            }
            ab1 += "</ul>";
            document.getElementById("opgeslagen").innerHTML = ab1;
        }

        function myFunction2() {
            document.getElementById("wisselspelers").value = "";
        }
        

        
        
        function gekozenFunction() {
        var rand = Math.floor(Math.random() * spelers.length);
        var concat = spelers[rand];
            document.getElementById("gekozen").innerHTML = (concat);
        }

    </script>

(I've placed the random selection inside the gekozen function so you select the random value from the most up to date spelers, maybe the original problem was that it was too early to choose a value)

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't, i get the undefined error edit after putting the random selecting inside the function it did work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.