Discover the method to adjust the position of elements, such as images, using radio buttons

I have an image positioned in a container along with 3 radio buttons. I want to make additional images appear over the main image when specific radio buttons are selected. Each button will trigger different positions for the additional images.

So far, this is what I have:

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default">
        <input type="radio" id="q156" name="quality[25]" value="1" /> 1
    </label> 
    <label class="btn btn-default">
        <input type="radio" id="q157" name="quality[25]" value="2" /> 2
    </label> 
    <label class="btn btn-default">
        <input type="radio" id="q158" name="quality[25]" value="3" /> 3
    </label> 
</div>
</div>
</div>
<div class="col-md-8 col-fl">
    <div id="img_box">
        <img style="width: 100%;" src="img/other/rounded_corners.png" />
    </div>

Answer №1

To enhance the radio button functionality, consider including image sources as data attributes.

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-default">
        <input type="radio" id="q156" name="quality[25]" value="1" data-img="http://placehold.it/350x150/ff0000" /> 1
    </label> 
    <label class="btn btn-default">
        <input type="radio" id="q157" name="quality[25]" value="2" data-img="http://placehold.it/350x150/00ff00"/> 2
    </label> 
    <label class="btn btn-default">
        <input type="radio" id="q158" name="quality[25]" value="3" data-img="http://placehold.it/350x150/0000ff" /> 3
    </label> 
</div>
<div class="col-md-8 col-fl">
<div id="img_box"></div>

The image box will appear empty but with a preset size and background image assigned through CSS:

#img_box{
    width: 350px;
    height: 150px;
    background: url('http://placehold.it/350x150/ff0000')
}

Next, create an event handler that appends an image overlay when a radio button is clicked (using the source specified in the data attribute):

 $(function(){
    $('input[type=radio]').click(function(){
        $this = $(this)
        $img_box = $('#img_box')
        $overlay = $('<img/>').attr('src',$this.data('img'))
        $img_box.html('').append($overlay)                    
    });
})

For a demonstration of this functionality, check out this example: https://jsfiddle.net/fmytsjv7/1/

Answer №2

To accomplish this task, you can utilize jQuery by creating an event handler that is bound to your radio buttons.

Here is an example:

$('input:radio[name="quality[25]"]').change(function(){ });

You can then add code within the handler to modify the attributes or CSS properties of either the target image container or the image element directly.

If you want to adjust the CSS properties of your target element, you can use jQuery's .css() method

For instance:

$("#img_box").css({"height":"100px","width":"100px",background:"red"});

Alternatively, if you need to change the attributes of your target element, such as the "src" attribute of an image, you can use jQuery's .attr() method.

For example: (in this case it updates the img's "src" attribute)

$("#img_box > img").attr("src","path/to/whatever/image");

Check out a demo on JSFIDDLE here

Since you mentioned the jQuery tag in your question, I assume you are looking for a jQuery-based solution.

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

Receiving information within an Angular Component (Profile page)

I am currently developing a MEAN Stack application and have successfully implemented authentication and authorization using jWt. Everything is working smoothly, but I am encountering an issue with retrieving user data in the Profile page component. Here ar ...

Creating a column in CSS that maximizes width without utilizing 100% or causing text wrap

Trying to keep a three-column div from spilling over the page can be tricky. The first two columns must not exceed 200px, while the third column should be at least 280px wide. The issue arises when there is a table in the third column with long sentences i ...

Tips on invoking Bootstrap's collapse function without using JQuery

We are facing a challenge with our TypeScript files as we have no access to jQuery from them. Our goal is to trigger Bootstrap's collapse method... $(object).collapse(method) but without relying on jQuery. Intended Outcome //Replicates the functio ...

Use Jquery to retrieve the innerhtml content

Hey there! I'm currently in the process of learning Jquery and have encountered an issue with accessing form elements. My script is designed to open a div and then fill it with a predefined HTML form. To achieve this, I'm using ajax to fetch the ...

Dealing with images on my live React application

For managing static images in my react app, I have integrated cloudinary as a CDN service. Can anyone suggest a seamless way to switch between using local image folders during development and switching to the CDN URL for production efficiently? ...

Incorporating a spectrum of hues into an HTML document

As a beginner in HTML, I recently stumbled upon a website (YT video) showcasing a stunning array of colors on buttons. You can check it out here: https://www.youtube.com/watch?v=OPaLnMw2i_0 I'm curious if these colorful designs can be applied to text ...

Showing a coordinated timer countdown alongside automatic server-side logout

I found inspiration in this particular PHP session timer example that incorporates an AJAX call to monitor the session's 'time'. I aim to integrate a simple JavaScript countdown timer for the user, like so: function startTimer(duration, di ...

Retrieve the current system datetime with just a click of a button

Does anyone know how to use JSF + RichFaces to automatically display the current date and time in an inputText field when a button is clicked? Any guidance on this would be greatly appreciated. Thank you! ...

The actions performed within an event listener function are undone once they have been carried out

I'm a newcomer to Javascript and I am currently experimenting with it. While browsing through a tutorial on w3schools, I came across an example of changing the color of a button after clicking on it. My idea was to take it a step further by loading a ...

Creating dynamic qtip2 tooltips is an effective way to enhance user interaction on

I am facing an issue where all tooltips work perfectly fine when the page loads, but stop working after data refreshes through ajax. How can I resolve this problem? $(document).ready(function() { // MAKE SURE YOUR SELECTOR MATCHES SOMETHING IN YOUR HT ...

Determining age in a Class upon instance creation

Is there a way to encapsulate the age calculation function within the Member Class without resorting to global space? I want to automatically calculate and set an age when creating an instance. Currently, I have a function that works, but I'm curious ...

What steps can be followed to incorporate a loading gif into this popup and subsequently eliminate it?

These simple inquiries are starting to get on my nerves. I can't keep waiting around for my website designer to resolve this issue. It shouldn't be that difficult... I managed to figure out most of it by researching similar questions, but I could ...

Harnessing the Power of Script Loader in Your webpack.config.json File

I'm taking my first steps into the webpack world with Vue.js and vueify for ES6 modules. I've run into a challenge when it comes to loading third-party jQuery plugins. I've successfully used the ProvidePlugin to load jQuery. plugins: [ ...

Ways to capture all characters (including special characters like ) using Visual Studio Regex

What I'm Hoping to Achieve: I am looking to reformat multiple HTML files that have a similar structure in Visual Studio. While the HTML sections may vary in length, they all begin with a <div id="some_id"> tag and do not contain any other <d ...

The for loop is throwing an error because the variable 'i' is not defined

I recently started using eslint and I'm in the process of updating my code to comply with my eslint configuration. module.exports = { env: { browser: true, commonjs: true, node: true, es2021: true, }, extends: 'eslint:recomm ...

How to conceal table cells on Safari 5?

My code works on Firefox and IE, but not on Safari. Here's an example: <table> <thead> <tr> <th style="display: none;">hi</th> </tr> </thead> <tr class="someClass"> <td style= ...

Issues encountered when selecting table data for filtering

One issue I am facing is with my HTML table that contains a lot of data. I have created a select option to filter the table and display the filtered data. The select options are based on the "route" column, with only three options available: Marikina, Mont ...

The <span> tag creating a surprise gap following a word

Using Angular4, I've come across an odd issue where one of my span elements is adding an extra space to the end of words like this: https://i.stack.imgur.com/H4TD7.png The only CSS properties that are set include font-family, -webkit-font-smoothing, ...

Developers guide to using Selenium IDE

After downloading the source code of Selenium IDE, I'm eager to make some edits but unfortunately, I can't seem to find any documentation on how to do so. Hey folks, anyone aware of any resources available for developers working on Selenium IDE? ...

Is it possible for a script with ignore_user_abort(true) to be halted by using the exit() function in PHP?

I've implemented a script that begins with: ignore_user_abort(true) This script is responsible for sending out emails. The reason for using ignore_user_abort(true) is because I'm accessing the page via AJAX, and there's a possibility that t ...