Switch the displayed image(s) based on the radio button selection made by the user

I need help implementing a feature on my website where users can change an image by selecting radio buttons. For example, there are three options:

1) Fox 2) Cat 3) Dog

When the user chooses "Fox," I want an image of a fox to be displayed. If they then switch to "Cat," I want the fox image to transition into a cat image. I have seen examples of changing background colors with radio buttons, but not images.

Example of Background Color Change: http://jsfiddle.net/6jt8h/

HTML Code:

    <div id= "genre">
What do you bust a move to?
<br>
<br>
<form name="music" method="post" action="">
<p>
<input type="radio" name="music" value="radio" onClick="changeColour('b')">Blues
<br>
<input type="radio" name="music" value="radio" onClick="changeColour('r')">Rock
<br>
<input type="radio" name="music" value="radio" onClick="changeColour('p')">Pop
<br>
</form>
</div>

Java Scipt:

    window.changeColour = function(value)
{
    alert(value);
    var color = document.body.style.backgroundColor;
    switch(value)
    {
        case 'b':
            color = "#FF0000";
        break;
        case 'r':
            color = "#0000FF";
        break;
        case 'p':
            color = "#FF00FF";
        break;
    }
    document.body.style.backgroundColor = color;
}

Any assistance would be greatly appreciated.

Thank you

Answer №1

I made some adjustments to your fiddle based on your requirements: check it out here

Here is the updated JavaScript code:

window.changeImage = function(value)
{
    var imageURL;
    switch(value)
    {
        case 'bird':
            imageURL = "https://upload.wikimedia.org/wikipedia/commons/9/93/House_sparrow04.jpg";
        break;
        case 'fish':
            imageURL = "https://upload.wikimedia.org/wikipedia/commons/4/46/Goldfish3.jpg";
        break;
        case 'rabbit':
            imageURL = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Rabbit_in_montana.jpg/857px-Rabbit_in_montana.jpg";
        break;
    }
    document.getElementById("myPicture").src=imageURL;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The new experimental appDir feature in Next.js 13 is failing to display <meta> or <title> tags in the <head> section when rendering on the server

I'm currently experimenting with the new experimental appDir feature in Next.js 13, and I've encountered a small issue. This project is utilizing: Next.js 13 React 18 MUI 5 (styled components using @mui/system @emotion/react @emotion/styled) T ...

Stop the bootstrap navbar from resizing

I implemented a bootstrap navbar that looks like this... <nav class="navbar navbar-inverse"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">WebSiteName</a> </div& ...

Navigate to a particular date with react-big-calendar

I know this might sound like a silly question, but I am new to working with React. Currently, the React-big-calendar allows users to navigate to specific dates when selected from the Month view. What I want is for the same functionality to apply when a use ...

Checkbox Trouble: Troubleshooting AJAX Integration

One of the requirements I have is to implement a checkbox list that updates the page via AJAX when one of the checkboxes is clicked. $(document).on("click", ".selectlist input", update_results); My client has requested that one of the checkboxes be pre-s ...

Transform chosen images into a slideshow using Node.js and Angular JavaScript

I'm currently working on a project where I have an array of images being printed out in Angular, which I can select and download as a zip file. However, I now want to modify this functionality to create a slideshow instead. I've already imported ...

Retrieve the Firepad textarea element or the current character being typed

I'm looking to add hashtag functionality to Firepad using jQuery. Is there a way to access the textarea element or the character being typed in the Firepad editor? For example, can I trigger an event when typing '#'? I attempted to use the ...

Utilize Object by referencing a string

I am trying to access an object from my node backend in React by using a string as a key. For example, if the string is 'Cat', I want to access the Cat object along with its corresponding key/value pairs. Here is an illustration of what the code ...

Can we switch the country name to the database name in Jvector?

I've successfully implemented the Jvectormap application and it's functioning as expected. When I click on a country, it displays the name of that country. I have established a simple database for specific countries. Through AJAX, I've conn ...

How to enhance a jQuery tab control by implementing custom JavaScript forward and back arrows?

Although there are other questions related to this topic, mine is unique. I have a scrolling jQuery feature that utilizes tabs for selection and is automated. Modifying the layout is challenging because it is dynamic and depends on the number of items in t ...

Shape with smoothed corners

Is there a way to create rectangles with rounded corners using CSS, SVG, or other methods while maintaining straight horizontal and vertical sides? I attempted to use border-radius for the rounded corners, but found that it created curved edges on both ho ...

Verify the functionality of the input fields by examining all 6 of them

I'm facing a challenge with a validation function in my project that involves 6 input fields each with different answers. The inputs are labeled as input1 to input6 and I need to verify the answers which are: 2, 3, 2, 2, 4, and 4 respectively. For e ...

Is there a way to invoke a client-side function from the server?

Is there a way to display an alert at the top of the browser if the SQL query returns empty results? I tried using the "alert" function, but I'm struggling with customizing its appearance. I have a function in my HTML code that triggers an alert, but ...

Transform JSON query conditions into MongoDB/Mongoose commands

I'm currently developing an application using Angular 8 on the front end and NodeJS 12 with MongoDB 4 / Mongoose 5 on the back end. Using the Angular query builder module, I have generated a query in JSON format. This JSON object will be sent to the ...

Having trouble submitting the edit form

I had the idea to create an edit form that would replace the existing data in a table for editing. However, I am facing issues with getting the form to submit properly even though the create form is functioning correctly. Below is the code snippet that I n ...

Removing elements from an array within a function

Within my class, I defined an array: myUsers = new Array(); I am looking to add usernames to this array in a unique manner. My approach involves checking if the array length is 0 and if so, adding a user. This method works well for adding the first user ...

The power of selenium meets the functionality of Javascript with the webdriver

Encountering an issue with Selenium JS Components are created in JSON format as follows: "usernameInputField": { "selector": { "xpath": "//*[@id='username']" } } For invoking webdriver, the following is used: var webdriver = ...

Using Angular template to embed Animate CC Canvas Export

Looking to incorporate a small animation created in animate cc into an angular template on a canvas as shown below: <div id="animation_container" style="background-color:rgba(255, 255, 255, 1.00); width:1014px; height:650px"> <canvas id="canv ...

Tips for modifying a list item on a different page

The Scenario When using Sharepoint 2010, I am able to select an item from a list: This action then displays the Read/Edit view on that page: My Objective I have a WebPart located on a different Page where I display items from various lists, including t ...

Creating Spinning Text Effects in Internet Explorer with CSS

Inside a list item (<li>), I have some text that I want to rotate 270 degrees. Direct methods in FireFox, Safari, Chrome, and Opera work fine for this, but when it comes to IE, there is no direct support. I've tried using filters like matrix and ...

yii2 truncates CSS classes in the email templates

My email sending process involves using a template: $content='Message'; Yii::$app->mailer->compose('@app/mail/templates/templ', ['content'=>$content]) ->setFrom('<a href="/cdn-cgi/l/email-protection" c ...