change the content class when an image link is clicked

I am looking to create a similar website concept like the one found at

The idea is to have two images, and when clicking on an image, compare a specific content below the image in full width. This also needs to be responsive.

How can I achieve this? The HTML markup for this concept is as follows:

<div class="imagecontainer"></div>

<div class="image1"></div>
<div class="image2"></div>
</div>


<div class="contentcontainer"></div>

<div class="content1"></div>
<div class="content2"></div>
</div>

Update

I have tried every code posted but nothing seems to work on my page. Here is what the page looks like:

<?php
if ( !defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
?>
<?php if ( have_posts() ) : ?>
    <div class="row archive-courses course-list archive_switch" itemscope itemtype="http://schema.org/ItemList">

        <?php
        // Start the Loop.
        while ( have_posts() ) : the_post();
            learn_press_get_template( 'archive-course-content.php' );
        endwhile;
        // Previous/next page navigation.

        ?>
    </div>
    <?php
    learn_press_course_paging_nav();
endif;
?>

No matter what I try, there always seems to be an issue with implementation.

Answer №1

Check out this sample code for an interactive element:

$('li').click(function(){
  $(this).parent().find('.selected').removeClass('selected');
  $(this).addClass('selected');
  $('.content').hide();
  
  
  var content = $(this).attr('ref');
  $('#'+ content).show();
});
ul {
  width: 100%;
}
ul li {
  width:100px;
  height: 100px;
  display: inline-block;
  border-radius: 100px;
  text-align: center;
  background-color: #ccc;
  line-height: 100px;
  vertical-align: middle;
  cursor: pointer;
}

ul li:hover,
ul li.selected {
  background-color: #eee;
}



div.content {
  display: block;
  width: 300px;
  height: 100px;
  background-color: yellow;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<ul>
    <li ref="div-1" class="selected">Button 1</li>
    <li ref="div-2">Button 2</li>
<ul>
  
<div>
    <div id="div-1" class="content">Div 1</div>
    <div id="div-2" class="content" style="display: none;">Div 2</div>
</div>

Answer №2

Simply click on the image to switch content visibility. I have created a snippet that transitions the content with a fade effect. You can easily add more images and content following the same logic.

$(document).ready(function(){

            $('.image').on("click",function(){
                if(!$(this).find("img").hasClass("selected")){
                    $(".image img").removeClass("selected");
                    $(this).find("img").addClass("selected");

                    $('.content').hide();
                    $("."+$(this).attr("rel")).fadeIn();
                }
            });

        });
.image > img{
            padding: 50px;
            display: inline;
            float: left;
        }

        .selected{
            opacity: 0.5;
        }

        .content{
            float: left;
            border: 1px solid black;
            width: 50%;
            display: inline;
            padding: 10px;
            margin: 10px;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div class="imagecontainer"></div>

        <div class="image" rel="content1"><img width="100px" class="img1 selected" src="http://www.cliparthut.com/clip-arts/124/pink-circle-clip-art-124566.png"></img></div>
        <div class="image" rel="content2"><img width="100px" class="img2"  src="http://www.cliparthut.com/clip-arts/124/pink-circle-clip-art-124566.png"></img></div>



        <div class="contentcontainer">

        <div class="content content1">HELLO content 1</div>
        <div class="content content2" style="display:none;">HELLO content 2</div>
        </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

In WordPress, the magic_quotes feature is essential for json_decode to function properly

In my current project on the client side, I am dealing with an array of JavaScript objects. Upon submission, this array needs to be sent to PHP using a form and then further manipulated on the server-side. As I work on building or modifying the array of o ...

Tips for adding a "Select All" feature to a dropdown list?

Currently, I have a dropdown list with a filter for IN and OUT values. The functionality is working as expected: <select class="form-select" style="max-width: 100px" [ngModel]="selectedBrand" (ngModelChange)="onChangeT ...

Tips for positioning an h4 and image element in a navigation bar

I have a homework assignment that requires creating a navigation area, with a collapsible button, where a div containing an image and heading comes before the button. My main goal is to align the image and heading side by side, and ensure they do not disa ...

Using PHP to send multiple data through ajax

Currently, I am facing an issue with jQuery where I am unable to pass more than one selected data from "Forms.html" to be sent to "ajaxServer.php" using jQuery "ajax.js". The problem arises when trying to receive multiple values at "ajaxServer.php", as one ...

What is the best way to showcase a JSON object in an attractive format on a webpage?

Similar Question: JavaScript data formatting/pretty printer var theobject_string = '{...}'; // I have a JSON object as a string. Is there a way to present this string in a visually appealing manner on an HTML webpage? I would like the ...

Implementing event listeners with Angular UI-Router

Currently, I am working with Angular 1.4.5 and Angular Ui Router. I am facing an issue where I am trying to utilize addEventListener to execute a function when a text field is blurred. The challenge I am encountering is that during the load state, the elem ...

What is the best way to open Chrome Developer Console using Selenium?

While conducting automated tests with C#, Webdriver, and Chrome, I was exploring the use of the performance.timing.domComplete JavaScript console function through the chrome web developer tool. Here is an example of how I attempted to incorporate it into m ...

Unable to submit with the enter key using vue-btn

I am have a common login form with a submit button at the end, currently requiring me to click the button with my mouse to submit. However, I would like to be able to simply press the enter key to submit the form. Here is the code for my button: <v-bt ...

Show only the results that have identifiers matching the parameter in the URL

My goal is to filter objects based on a URL parameter gatewayId and display only those whose id matches the parameter. import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; @Component({ selector ...

Using both ng-if and ng-click in AngularJS for enhanced functionalities

There's a button that should display a toggle only when the value is greater than 0; otherwise, it shouldn't do anything. This snippet of code shows how things were prior to adding ng-if: <span >{{values.valuesNumber}} <i class= ...

The error message "404 Not found" is returned when attempting to retrieve a Google spreadsheet

I have exhausted all of my options on the web trying to retrieve my Google spreadsheet as a json file. $.getJSON() However, I am constantly receiving a 404 Not Found error. When I publish to the web, the URL I get is: https://docs.google.com/spreadsheets ...

Pointer-events property in pseudo elements not functioning as expected in Firefox browser

I have a horizontal menu that includes mid-dots (inserted using the ::after pseudo element) to separate the menu items. I want to prevent these mid-dots from being clickable. This works fine in Safari, but in Firefox (v.47), the pointer-events: none prope ...

D3 and dimple line graphs

As I continue to explore D3, I am working on a project where I need to display data using the Dimple module for charting. The user should also have the ability to add parameters. I have experimented with two different ways of formatting my data, and I hav ...

Error: The function loadJSON has not been declared

Currently utilizing the Atom text editor, I have successfully installed node.js along with npm install -g json. The installation process was smooth and the version information displayed correctly in the command prompt window. Running a server using nodemon ...

Instructions for printing a page and closing the Print window using Selenium

New to using Selenium: I tried the following code in Selenium to open the print window, but unfortunately it doesn't seem to work Keyboard keyboard = ((HasInputDevices)driver).getKeyboard(); keyboard.pressKey(Keys.ENTER); keyboard.pressKey(Keys.ch ...

Is it possible to retrieve the current selector as a string in LESS?

Is there a way to utilize the name of my css classes to specify the URL for an image they depend on? I know that using an ampersand within a selector allows you to add additional states, but can it be accessed as a string? For example: .my-image{ back ...

How to bind parent object scope to MySQL query's anonymous callback in Node.js

As a newcomer to node.js, I am still working on understanding the concept of scoping anonymous functions. I have created an object with multiple methods and within one of my methods, I retrieve a MySQL connection from a pool and then execute a query on tha ...

Creating a user-specific subdomain in node.js: A step-by-step guide

I am interested in sharing user information at username.domain.com within my application. The subdomain should only be available after the user creates their account. I have come across a module that seems promising for this task: Express Subdomain How d ...

Errors may occur when attempting to auto-refresh a page with a PHP-generated image using Ajax

When I include the image somepage.php in my code, it displays correctly. However, if I use Ajax to refresh the div containing somepage.php, the text becomes distorted. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

Is it feasible to extract the Json object from its current location within another object?

Is there a way to manipulate this JSON data? "forms": [ { "_id": "Untitled Form", "title": "Untitled Form", "answer": [ { "username&quo ...