Dynamic text modification through Bootstrap click event

Here is the coded implementation using bootstrap:

<div class="map-images">
            <img src="images/map/level0.png" title="image 1" alt="Ground floor" id="image1" class="image-toggle"/>
            <img src="images/map/level1.png" title="image 2" alt="floor 1" id="image2" class="image-toggle" style="display:none;"/>
            <img src="images/map/level2.png" title="image 3" alt="floor 2" id="image3" class="image-toggle" style="display:none;"/>
            <img src="images/map/level3.png" title="image 4" alt="floor 3" id="image4" class="image-toggle" style="display:none;"/>
        </div>
        <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-primary image-toggler" data-image-id="#image1" data-link="first">
                <input type="radio" name="options" id="option1" href="#lvl0-overview"> Ground Floor
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image2" data-link="second">
                <input type="radio" name="options" id="option2" href="#lvl1-overview"> Floor 1
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image3" data-link="third">
                <input type="radio" name="options" id="option3" href="#lvl2-overview"> Floor 2
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image4" data-link="fourth">
                <input type="radio" name="options" id="option4" href="#lvl3-overview"> Floor 3
            </label>
        </div>

The corresponding JavaScript code:

<script type="text/javascript">
    $('.image-toggler').click(function(){
        $('.image-toggle').hide();
        $($(this).attr('data-image-id')).show();
    });
</script>

The current functionality involves changing the displayed image based on button clicks. The goal is to include an onclick feature that will reveal a floor description below the buttons.

A layout example of what is needed:

-------------
| Floor      |
| overview   |  <-image swapping here based on button click
|            |
|            |
-------------
button1 button2 button3

--------------
| floor       |
| description | <-Change divs based on the current floor.
|             |
|             |
--------------

Specifically seeking advice on incorporating this using CSS and additional resources. Attempted by adding href tags to <input> elements, but encountering issues. What steps should be taken next?

Answer №1

Ensure that there are no CSS properties hiding the images from view

(for example, visibility:hidden or opacity:0)

Take a look at this method using event delegation:

$(document).on('click', '.image-toggler', function() {
  $('.image-toggle').hide();  
  $($(this).attr('data-image-id')).show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="map-images">
            <img src="images/map/level0.png" title="image 1" alt="Ground floor" id="image1" class="image-toggle"/>
            <img src="images/map/level1.png" title="image 2" alt="floor 1" id="image2" class="image-toggle" style="display:none;"/>
            <img src="images/map/level2.png" title="image 3" alt="floor 2" id="image3" class="image-toggle" style="display:none;"/>
            <img src="images/map/level3.png" title="image 4" alt="floor 3" id="image4" class="image-toggle" style="display:none;"/>
        </div>
        <div class="btn-group" data-toggle="buttons">
            <label class="btn btn-primary image-toggler" data-image-id="#image1" data-link="first">
                <input type="radio" name="options" id="option1" href="#lvl0-overview"> Ground Floor
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image2" data-link="second">
                <input type="radio" name="options" id="option2" href="#lvl1-overview"> Floor 1
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image3" data-link="third">
                <input type="radio" name="options" id="option3" href="#lvl2-overview"> Floor 2
            </label>
            <label class="btn btn-primary image-toggler" data-image-id="#image4" data-link="fourth">
                <input type="radio" name="options" id="option4" href="#lvl3-overview"> Floor 3
            </label>
        </div>

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

How far apart are two surfaces when they are rotated along the Y-axis

Access Code: Click here to access the code I am trying to make sure that the red surface lies completely flat on top of the gray surface. However, there seems to be a gap (marked ??) between the two surfaces when I rotate 'modifier1'. How can I ...

Display Issue: No Uploadify Alert Appears After File Upload

I currently have the following code for using uploadify: <link href="/uploader/uploadify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="/uploader/jquery-1.5.1.js"></script> <script type="text/javascript" ...

Struggling with a CSS problem that jQuery can't seem

I recently experimented with Infogrid on my website. After adding a header and footer, I noticed that the text in the last block of iron man was being cut off. Even though I removed overflow:hidden; from the body and html, the issue persisted with the te ...

Can icons with an external path be utilized as a src in the manifest.json file within an Angular application?

Let's visualize the setup with two projects - project1 and project2. Each project has its own manifest.json file and the same apple-touch-icon-144x144.png file located in assets/icons directory. -project2 |_src | |_assets | | |_icons | | ...

The functionality of .on is disrupted by a dynamically generated table through ajax

I am currently using ajax to dynamically generate a table. Once a user inputs a query, a new table containing data is created and replaces the existing content within #content-display: function searchQuery(query){ $.ajax({ url: "search.php", ...

js simulate a click on an anchor element based on the child element's id

I am trying to automatically trigger a click on an a tag that contains a div with the id='a'. $(document).ready(function() { $("#chat_list_content a:has(div[id='a'])").click(); //$("#chat_list_content a:has(div[id='a']) ...

The CSS file is not displaying the background image on the PHP file

I am facing an issue with using CSS to include a background image for the layer, but it doesn't seem to be working correctly... The CSS code snippet is as follows: header { background-image: url("boisko.jpg"); background-repeat: no-repeat; background ...

`In NodeJS, facing a challenge with implementing a many-to-many relationship using Sequelize's

I'm in the process of setting up many-to-many relationships between roles and accesses. The `roles` table will contain a list of roles (admin, developer, etc...) and the `accesses` table will have a list of permissions (Create Project, Create Site, De ...

The <h:outputStylesheet> tag fails to display any content on the HTML page

I am having trouble getting my CSS sheet to load while using JSF2 and PrimeFaces. The CSS file is located at WebContent/resources/css/style.css Here is the xhtml code: <h:head></h:head> <h:body> <h:outputStylesheet name="css/styles. ...

What is the best way to halt the current event handler thread's execution when another event is triggered that calls the same handler at

One of the functions in my code filters and sorts contents of a select dropdown based on input text entered by the user. The function filterSort() is triggered on each keyup event from the input field. Code $(inputTextField).keyup(function() { / ...

Can a custom JavaScript variable string be inserted into a Flask variable?

TL;DR --> Can I utilize JavaScript variables to access Python variables from a Flask app within Jinja {{ }} tags? Instead of repeating similar JS code blocks for different city variables with slightly different names, like: //PTC Color Change if ({{ ...

Express.js never terminates a session

I have a Backbone View that makes an Ajax call to the server to delete a session. Upon triggering the following event on the server: app.delete('/session', function(req, res) { if (req.session) { req.session.destroy(function() { ...

Steps for eliminating an element upon the second click:

I am faced with a challenge where I need to dynamically add elements to a container on the first click and delete them on the second click. I feel like I am complicating things unnecessarily and there must be a simpler and more elegant solution available. ...

Manipulating classes with JQuery based on browser window size

Looking for assistance in creating a short script that can dynamically add or remove classes based on window width. Any guidance on this matter would be greatly appreciated. <script type="text/javascript"> $(document).ready( function() { if ( ...

Guidance on navigating to a different page using a specific identifier

I'm a beginner in Angular, currently working on developing a full stack MEAN application. I need some guidance regarding an issue I'm encountering. My goal is to create a form called 'fa-daform.component.html' that captures a brief desc ...

Unspecified Values in Jquery

Here is the code snippet I am working with: <div class="row"> <div class="col-xs-12 col-md-12"> <p class="lead"> $21.000 </p> </div> <div class="section col-xs-12 col-md-12" style="p ...

Tips on how to send Mongoose response variable in NodeJS Express router res.render?

I just finished setting up a basic Express NodeJS server. My goal is to save the value of the setting returned from mongoose.find() as a variable in res.render, but every time I try, it throws an error saying Cannot read property of undefined. Here' ...

Troubleshooting issues with jest.mock in JavaScript testing when using TypeScript modules

I'm facing an issue where my mocked utilFunction is not being utilized. Upon adding logging to the factory function, it becomes apparent that the function is never called. I have attempted to troubleshoot by searching for reasons such as jest.mock not ...

Bootstrap encountered an issue with altering header information

I've been trying to set up a notification system that plays a sound every time someone makes a new post. I'm working with Bootstrap 4 and I've tried inserting JavaScript into the functions page, but no matter how many times I call the ' ...

The page is reloading and redirecting to a new page with a JSON response after the submit button is clicked, all

My goal is to use AJAX for inserting and fetching data from the database without reloading or redirecting to another page. However, when I submit the form, it redirects me to a page displaying the JSON response instead of staying on the current page. It se ...