Modify the state of each individual button

I recently came across a cool implementation using clicks and jQuery fade effects to show a div when a button is clicked and then change the state of the button. Check it out here: http://jsfiddle.net/ttj9J/5/

HTML

<a class="link" href="#" data-rel="content1"><img src="http://i.imgur.com/vi1KLp9.png"></a>
<a class="link" href="#" data-rel="content2"><img src="http://i.imgur.com/u1SbuRE.png"></a>
<a class="link" href="#" data-rel="content3"><img src="http://i.imgur.com/u1SbuRE.png"></a>
<a class="link" href="#" data-rel="content4"><img src="http://i.imgur.com/u1SbuRE.png"></a>
<a class="link" href="#" data-rel="content5"><img src="http://i.imgur.com/u1SbuRE.png"></a>

<div class="content-container">
    <div id="content1">This is the test content for part 1</div>
    <div id="content2">This is the test content for part 2</div>
    <div id="content3">This is the test content for part 3</div>
    <div id="content4">This is the test content for part 4</div>
    <div id="content5">This is the test content for part 5</div>
</div>

CSS

.content-container {
    position: relative;
    width: 400px;
    height: 400px;
}
.content-container div {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}

JQUERY

$(".link").click(function(e) {
      e.preventDefault();
      $('.content-container div').fadeOut('slow');
      $('#' + $(this).data('rel')).fadeIn('slow');
      $(".link img").attr("src", "http://i.imgur.com/u1SbuRE.png");
      $(this).find("img").attr("src", "http://i.imgur.com/vi1KLp9.png");
});

$( document ).ready(function() {
    $(".link")[0].click(); 
});

Although this setup works well, I am looking to have a different button with a unique pressed button image for each of the five buttons. Can anyone provide some guidance on this?

Answer №1

Check out the DEMO here!

function switchImage1() {
    $(this).find("img").attr("src", "http://i.imgur.com/vi1KLp9.png");
    $(this).one("click", switchImage2);
}

function switchImage2() {
    $(this).find("img").attr("src", "http://i.imgur.com/u1SbuRE.png");
    $(this).one("click", switchImage1);
}
$(".link").one("click", switchImage1);

Another way of achieving the effect using CSS classes

Removed img tag and only used a tags for simplicity.

HTML structure:

<a class="link active" href="#" data-rel="content1"></a>

<a class="link" href="#" data-rel="content2"></a>

<a class="link" href="#" data-rel="content3"></a>

<a class="link" href="#" data-rel="content4"></a>

<a class="link" href="#" data-rel="content5"></a>

CSS rules:

.link {
    content:url("http://i.imgur.com/vi1KLp9.png");

}
.active {
    content:url("http://i.imgur.com/u1SbuRE.png");
}

JavaScript functionality:

$(".link").click(function () {
    $(this).toggleClass('active');
});

After a comment from OP, updates were made to the code:

Check out the updated DEMO!

Updated JavaScript code:

 $(".link").click(function (e) {
     e.preventDefault();
     $('.content-container div').fadeOut('slow');
     $('#' + $(this).data('rel')).fadeIn('slow');
     $('.link').removeClass('active');
     $(this).toggleClass('active');
 });

 $(document).ready(function () {
     $(".link")[0].click();
 });

Updated CSS rules:

.link {
    content:url("http://i.imgur.com/u1SbuRE.png");
}
.active {
    content:url("http://i.imgur.com/vi1KLp9.png");
}

Answer №2

Consider using a background-image instead of a regular image for your buttons. You can apply an .active class and set the background-image using CSS for all your buttons like this:

.link[data-rel="content3"] .active {
   background-image: url(....);
}

Note:

http://jsfiddle.net/ttj9J/23/

You can now include the classes as mentioned above.

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

jQuery plugin that controls scrolling speed using the mousewheel

I am trying to replicate the header design of Google+ which includes a search bar that moves when scrolling. Specifically, when the user scrolls down, the search bar shifts to top:-60px and the second horizontal menu shifts from top:60px to top:0 becoming ...

What is the process for extracting data from a canvas tag and why does it appear invisible on my browser?

The highlighted area in this image contains the HTML content and the red circle marks the section to be scraped The phone number can be found within the canvas tag. However, when trying to scrape the tag, it shows "Your browser does not support the HTML5 c ...

Inquiry regarding the grid structure within my react application

When attempting to arrange my images in a grid layout, I encountered a strange issue after applying CSS grid. The grids ended up being organized horizontally instead of how I wanted them to be - two or three images per line. The component hierarchy is as ...

Properly appears in Chrome, but experiences issues in Internet Explorer 11

The design of the website looks seamless in Chrome and Firefox, however, when I view it in Internet Explorer, I notice a significant gap on the right side of the first two pages. How can I fix this issue specifically for Internet Explorer? Should I use a m ...

I am in search of a regular expression to validate a Jordanian phone number, whether it includes the country code or not

Looking for a regex pattern that can validate phone numbers beginning with either 0096279, 0096278, 0096277, or 079, 078, 077. ...

Issue with the progress bar bouncing back and forth

I am encountering an issue with my progress bar width changing on nav-links click. When I click on each nav-link, the progress bar reaches its width correctly but then turns back and forth unexpectedly. I am unsure why this is happening. Here is the code ...

The jQuery .load() function does not seem to be functioning properly on secure HTTPS connections

After implementing an SSL certificate through Cloudflare on my website, I encountered an issue where a specific function returned an error code 0 and failed to load the URL content. How can I resolve this issue? $(function () { EnderReact(); }); functi ...

What causes the HTML element's X position value to double when its X position is updated after the drag release event in Angular's CDK drag-drop feature?

I am facing a challenge with an HTML element that has dual roles: Automatically moving to the positive x-level whenever an Obsarbalve emits a new value. Moving manually to both positive and negative x-levels by dragging and dropping it. The manual drag a ...

Creating a jQuery plugin with configurable options but without attaching it to any specific HTML element can be

I am currently working on developing a unique plugin that does not require a DOM element but comes with default options pre-set. However, upon executing the code snippet below, I encounter the error message TypeError: $.myApp is not a function. What modi ...

Assign a class to an element depending on the date

I need to customize a span element in my HTML code like this. html <span class="tribe-event-date-start">September 5 @ 7:00 pm</span> My objective is to identify that specific element based on its date and then apply a class to its parent co ...

Creating a currency input field in HTML using a pattern textbox

In a project using HTML, Angular 2, and Typescript, I am working with a textbox. How can I ensure that it only accepts numbers along with either one dot or one comma? The input should allow for an infinite number of digits followed by a dot or a comma and ...

Attempting to incorporate a new CSS sub menu alongside the current one

Having trouble with menu styling, I'm new to this and trying to add a sub menu to an existing drop down menu. Check out my JSFiddle This is the CSS code: .menu_main { float: left; width: 60%; } #access .menu { list-style: none; font-weight: normal ...

Can an HTML DOM object be converted to a JSON string using JSON.stringify in JavaScript?

Trying to fetch an external HTML file and convert its body content into a string has been giving me unexpected results. Is there a way to achieve this successfully? var xhr = new XMLHttpRequest(); function loadFile(){ xhr.open("GET", 'index.html ...

Create an Angular directive that highlights a div if any of its child inputs have focus

I've developed an Angular directive for a repetitive section containing form elements. My aim is to have the entire section highlighted whenever any input field inside it is focused. template.html <div class="col-md-12 employee-section"> <l ...

Failure to send serialized data via AJAX to the specified URL

After executing the SUCCESS function, I have confirmed that my values are accurate: serviceID=123&acceptAgreement=on The acceptAgreement field is an input checkbox. Despite this, the DB file does not show any updates. To troubleshoot, I attempted to ...

An HTML document may display a segment of a URL

Is there a way to display part of a URL in an HTML document, so that if the URL changes, the corresponding content in the body also updates? For instance, www.example.com/?key=i - Is it possible for this link to be dynamically displayed within the body o ...

Having difficulty displaying data in the proper format with two-way binding

In the realm of my webpage, I have a plethora of headings, paragraphs, images, and other data at my disposal. From the backend, a dataset is provided to me that includes an array with various properties housing the desired information. The challenge lies i ...

Integrate jQuery-ui into your Angular 5 application

Having some trouble integrating jquery-ui into my Angular 5 project. I need to use a feature that requires jquery-ui, but I keep encountering this error: TypeError: WEBPACK_MODULE_10_jquery(...).droppable is not a function I initially attempted to plac ...

Is there a way to identify the moment when a dynamically added element has finished loading?

Edit: I've included Handlebar template loading in my code now. I've been attempting to identify when an element that has been dynamically added (from a handlebars template) finishes loading, but unfortunately, the event doesn't seem to trig ...

The inputs are not responding to the margin-left: auto and margin-right: auto properties as expected

I'm having trouble aligning an input in a form to the center. Even though I have included display: block in the CSS, using margin-left:auto and margin-right: auto is not yielding the desired result. To illustrate the issue more clearly, I've cre ...