I require the ability to switch between images similar to switching tabs on a menu bar

I am currently working on a billing application for the web and I require a tab menu bar structure. Each tab has 2 images to indicate active and inactive states. Below is the code snippet.

<html>
<head>
<style> 
.gold1 { position:absolute; top: 10px; left: 10px; z-index: 1; } 
.gold2 { position:absolute; top: 10px; left: 10px; z-index: 1; }

.gray1 { position:absolute; top: 10px; left: 10px; z-index: 1; }
.gray2 { position:absolute; top: 10px; left: 10px; z-index: 1; }


</style>
<script>
function changeImage() {
    var image = document.getElementById('myImage');
    if (image.src.match("gold1")) {
        image.src = "gray1.svg";
    } else {
        image.src = "gold1.svg";
    }
}
function changeImage1() {
    var image = document.getElementById('myImage1');
    if (image.src.match("gray2")) {
        image.src = "gold2.svg";
    } else {
        image.src = "gray2.svg";
    }
}
</script>
</head>
<body>

<img src="gray2.svg" id="myImage1" onclick="changeImage1()" class="gray2"/>

<img src="gold1.svg" id="myImage" onclick="changeImage()" class="gold1"/>


</body>

</html>

Currently, clicking on an image toggles between the two but I need it to effectively switch between active and inactive states.

edit 01

<script src="https://localhost/bg_out/jquery.min.js"></script>
<script>
$("#infoToggler img").click(function() {

    $(this).attr('src',$(this).attr('class')+'.svg');
});

This jQuery script has been added to achieve the desired functionality.

<div id="infoToggler">
<img src="gray3.svg" id="myImage2" class="gray2"/>
<img src="gray2.svg" id="myImage1" class="gray2"/>
<img src="gold1.svg" id="myImage"  class="gold1"/>
</div>

Here is the corresponding HTML file.

Answer №1

Here is an alternative approach that might be simpler:

Check out the live demo on Fiddle : http://jsfiddle.net/T7FWF/12/

Make sure to include jQuery library :

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">

<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>

    <div id="tabs"> 
  <ul> 
    <li><a href="#tab1"><img width='40' height='40' src="http://wiki.sugarlabs.org/images/7/7e/Gentoo-small.png" /></a></li>
    <li><a href="#tab4"><img width='40' height='40' src="http://www.acdivoca.org/site/Lookup/RSS-feed-small-PNG/$file/RSS-feed-small-PNG.png" /></a></li>
    <li><a href="#tab2"><img width='40' height='40' src="http://wiki.teamliquid.net/starcraft/images2/thumb/d/da/Dignitas.png/100px-Dignitas.png" /></a></li>
    <li><a href="#tab3"><img width='40' height='40' src="http://png-4.findicons.com/files/icons/1588/farm_fresh_web/32/arrow_refresh_small.png" /></a></li>
  </ul>
  <div id="tab1"><p> Content for tab 1 goes here </p></div>
  <div id="tab4"><p> Testing text </p></div>
  <div id="tab2"><p> Hey there! </p></div>
  <div id="tab3"><p> Inexpensive options </p></div>
</div>


$('#tabs').tabs();

.ui-tabs .ui-tabs-nav li.ui-tabs-active{
    background:gray;
}

.ui-tabs .ui-tabs-panel{
    background:gray;
}

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

Error: You forgot to include a name after the dot operator

I am facing an issue where I am unable to use YUIcompressor to compress a file. The script is running smoothly, but I am encountering the error missing name after . operator on line 3 of this script: Specifically at: "+ source.response.chars[k].name +" I ...

Follow these steps to position a child div absolutely within a grandparent div with the following structure

Is it possible to make the child div absolute for the grand parent div without changing the parent div's position? Here is the scenario: the parent div is a relative div, but the grand parent div is also a relative div. How can we achieve this without ...

When making an ajax call, I passed the data "itemShape" but on the URL page, an error appeared stating "Undefined index: itemShape"

Hello, I have been using an Ajax function to send data with the key itemShape. However, when I directly access the URL page or service page, it displays the following error: Notice: Undefined index: itemShape in C:\wamp64\www\inventory_so ...

Using JQuery Datepicker with an icon and custom format in ASP.NET

I am looking to incorporate jQuery into my Textbox. Specifically, I would like to use the Datepicker with the format yyyy-mm-dd and include an Icon as well. <script> $( "#txtVon" ).datepicker({ showOn: "button", buttonImage: "ima ...

Creating a responsive layout with Bootstrap4 to show two rows of three columns on smaller screens

I am looking to design a pagination feature that has the following appearance: On a wide screen: | | | [<<] Page # 1 of 6 [>>] | | ...

The jQuery menu is not working properly on Android's Chrome browser

When browsing http://creeight.se/stackExample1/, I noticed that the vertical dropdown menu functions properly in my desktop browsers (Chrome, FF, IE10), but experiences glitches when using Chrome for android. The menu utilizes .slideDown() to reveal submen ...

Exploring the html element through Xpath

I am currently attempting to locate an HTML element using Selenium XPath and C#. Here is the code snippet of what I have experimented with: var element = Driver.FindElement(By.XPath("//img[text()='logo-home.png']/@href")); if (element.Display ...

Unexpected behavior with Bootstrap popover template configuration

I'm currently in the process of customizing the template for the bootstrap popover. The initial code I found is as follows... $('.item').popover({ html : true, placement: 'left', content: function(){ return $(t ...

Ensuring JS consistently monitors changes in value

Is there an equivalent of (void update) in Unity that is called every frame in Web Development (using JavaScript)? "I want it to continuously check if certain values have changed and then update them accordingly." let governmentprice = parseFloat(select ...

The children elements inside a parent div with Flexbox are now taking on the height of their container

I am facing an issue with my flexbox grid layout where one column contains a tall image while another column has two shorter images stacked on top of each other. The problem arises when viewing the layout in Internet Explorer, as the height of the smaller ...

Having trouble with CakePHP's json_encode function for JSON responses?

I am currently using an ajax function to retrieve data for my jQuery autocomplete feature, however I am encountering an issue where the json response is not being parsed and I am unable to determine the cause. Upon inspecting the console logs for a workin ...

A step-by-step guide on initializing and setting a cookie value with expiration

Below is the code I have added. Can someone please help me locate the bug? Here is the code snippet: $_SESSION['browser'] = session_id(); setcookie($expire, $_SESSION['browser'], time() + (60 * 1000), "/"); echo "Value is: " . $_COO ...

When the parent div contains at least four divs, show the scroll arrow containers

In my code, there is a parent div that holds multiple child divs. If the number of child divs within the parent div exceeds 4, I want to show the scroll arrow containers. If it's less than 4, then those arrow containers should not be displayed. The ...

Enhance user experience with jQuery UI sortable and the ability to edit content in real

I am facing an issue with jquery sortable and contenteditable. The problem arises when I try to use jquery sortable along with contenteditable, as the contenteditable feature stops working. After searching on Stack Overflow, I found a solution. However, up ...

Move my site content to the appropriate location

I am working on a single-paged website built with Jquery and HTML. The main content is housed within a div called #site-content, which has a width of 4400px. Each "page" is positioned within this div, with the first page starting at left: 0px, the second a ...

"Using the power of jQuery to efficiently bind events to elements through associative

I am attempting to link the same action to 3 checkboxes, with a different outcome for each: var checkboxes = { 'option1' : 'result1', 'option2' : 'result2', 'option3' : 'result3', }; ...

Tips for hiding the overflow scrollbar on Microsoft Chrome when there is no content to scroll

Looking for a solution to hide scroll bars in Microsoft Chrome, but only when there is no content to scroll? The current div and styles always show the horizontal and vertical scroll bars, even when the height exceeds the content. <div style="backgroun ...

Reduce brightness in JavaScript - adjust downward

I stumbled upon this code snippet on Stack Overflow: function increase_brightness(hex, percent){ var r = parseInt(hex.substr(1, 2), 16), g = parseInt(hex.substr(3, 2), 16), b = parseInt(hex.substr(5, 2), 16); return '#' ...

What is the best way to determine the accurate size of a div's content?

There are 2 blocks, and the first one has a click handler that assigns the block's scrollWidth to its width property. There is no padding or borders, but when the property is assigned, one word wraps to the next line. The issue seems to be that scrol ...

Converting HTML Form Data into CSV with Multiple Rows

I currently have a form that generates a CSV file with data from select elements in the HTML form. The columns in the CSV correspond to the selected options in the form, but I need to extend this functionality to populate multiple rows in the CSV. Expecte ...