Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Code wont run properly. I've checked multiple times and there are no typos and anything. Can someone please help me out with this.

Amal Suleman

New Coder
JavaScript:
<html>
<body>
    <select id="colour">
        <option>red</option>
        <option>yellow</option>
        <option>purple</option>
        <option>orange</option>
        <option>green</option>
    </select>
    <form id="animals">
        <input type="radio" name="animals" value="elephant">Elephant <br>
        <input type="radio" name="animals" value="tortoise">Tortoise <br>
        <input type="radio" name="animals" value="dolphin">Dolphin <br>
        <input type="radio" name="animals" value="kangroo">Kangroo <br>
    </form>
    <button onclick="createAnimal ()">Create the animal</button>
    <script>
        function createAnimal () {
            var dropDownName = document.getElementById("colour");
            var colourChosen = dropDownName.options[dropDownName.selectedIndex].text;
            for(i=0; i<4; i++){
                if(animalRadio[i].checked) {
                    var animalChosen = animalRadio[i].value;
                }
            }
        }
        alert(colourChosen + " " + animalChosen);
        </script>
        </body>
        </html>
 
Maybe something like this

JavaScript:
<html>

<body>
    <select id="colour">
        <option>red</option>
        <option>yellow</option>
        <option>purple</option>
        <option>orange</option>
        <option>green</option>
    </select>
    <form id="animals">
        <input type="radio" name="animals" value="elephant" checked>Elephant <br>
        <input type="radio" name="animals" value="tortoise">Tortoise <br>
        <input type="radio" name="animals" value="dolphin">Dolphin <br>
        <input type="radio" name="animals" value="kangroo">Kangroo <br>
    </form>
    <button onclick="createAnimal ()">Create the animal</button>
    <script>
        function createAnimal() {
        var color = document.getElementById('colour');
        var colorid = color.options[color.selectedIndex].text;

        var animal = document.getElementById('animals');
        for(i=0; i< animal.length; i++){
            if(animal[i].checked) {
               pick = animal[i].value;
            }
        }
        alert('You picked a '+colorid+' '+pick);
        }
    </script>
</body>

</html>
 
Rather than correct your code, as @menator01 kindly did, I would urge you to run the debugger whenever some code does not work as it should. This would have immediately revealed the issues:

a.jpg
The first error happens when you load the page, the second when you click the button.
To run the debugger, press F12 and click the Console tab.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom