Connecting a button to a div using jQuery

Looking to connect a div with a button, where each div has its own corresponding button. The goal is to change the button color when the associated div is visible.

Here's my code attempt:

<ul style="padding-left:10em;">
    <li id="btID-1">1</li>
    <li id="btID-2">2</li>
    <li id="btID-3">3</li>
    <li id="btID-4">4</li>
</ul>
<div id="pgID-1" class="ui-content">content 1</div>
<div id="pgID-2" class="ui-content">content 2</div>
<div id="pgID-3" class="ui-content">content 3</div>
<div id="pgID-4" class="ui-content">content 4</div>

Jquery script:

 $(document).ready(function () {
     $('.ui-content').hide();
     $('#pgID-1').show();
     $(setPage);


     function setPage() {

         var active = $('.ui-content:visible');
         currPage = $('.ui-content:visible').index(active);


         currButton = $('.ui-dot').index(currPage);
         $(currButton).css("color:#ff0000");

     }

 });

I'm also looking to have the buttons control which div is shown and hide the rest. Any feedback on improving this approach is welcome!

Your insights are greatly appreciated.

Answer №1

It appears that the requirements are not completely clear, but the following code should meet them:

<ul id="btns" style="padding-left:10em;">
    <li id="btID-1">1</li>
    <li id="btID-2">2</li>
    <li id="btID-3">3</li>
    <li id="btID-4">4</li>
</ul>
<div id="pgID-1" class="ui-content">content 1</div>
<div id="pgID-2" class="ui-content">content 2</div>
<div id="pgID-3" class="ui-content">content 3</div>
<div id="pgID-4" class="ui-content">content 4</div>

 $(document).ready(function () {
     $('.ui-content').hide();
     $('#pgID-1').show();

     $('#btns li').click(function(){
         $('.ui-content').hide();
         $('#btns li').css("color","black");
         var IDno = $(this).attr("id").substr(5);
         $('#pgID-'+IDno).show();
         $(this).css("color","red");
     });

 });

To see this code in action, visit the JSFiddle link provided below:

http://jsfiddle.net/w5QJK/12/

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

The run() function in AngularJS inside an IFrame does not execute again when the IFrame is reloaded

In my webpage, there is a "Start Portal" button and an iframe with the id: MyFrame. Both elements are on the same webpage. I use jQuery AJAX to fetch the URL of my AngularJS based portal and then set this URL as the src for the iframe. The code snippet be ...

Using Jquery in asp.net to dynamically add and delete rows within a table

Having an organized table on the Master Page table border="1" border-style="dashed" width="80%" id="tblAddBirthdays" tr id="tr" td asp:TextBox ID="txFirstName" runat="server" asp:TextBox asp:TextBox I ...

Having trouble adding flexslider before a div element with jQuery

Hey there! I recently got flexslider from woothemes.com. The page structure I'm working with looks something like this: <div class="parentdiv anotherdiv"> <div class="child-div1">some buttons here</div> <div class="child-div2"& ...

The innerHTML property and &nbsp; entity

I'm encountering a strange JavaScript behavior that has me puzzled. Take a look at this code snippet: var el = document.createElement('div') var s = String.fromCharCode(160) el.innerHTML = s console.log(s) // prints space cons ...

Title of HTML Page: The Importance of Localization in Website Development

Two inquiries regarding an improved method of problem-solving: 1) Is there a way to alter the HTML page title for different client-side code locales without relying on javascript? Basically, changing the language of the title displayed in the browser tab. ...

What is the method to ensure span elements wrap text without setting a specific width or maximum width on the parent div?

Here is a snippet of code I have generated for displaying warnings or errors in Dojo dialogs: /* Relevant CSS styles */ .l-status-message-wrapper { height: auto; margin-left: 5px; margin-top: 2px; min-height: 15px; } .l-status-message-w ...

Issues with Rails not successfully sending AJAX data to the controller

I am new to AJAX, so please bear with me while I try to figure this out. My goal is to send data from a chat message box to two places – the chat box itself and to a method in my Rails backend. This way, I can display the message in real-time and also st ...

Trick for adjusting padding on top and bottom for responsive blocks of varying widths

Hello there, I've been working on a project where I need to create a responsive grid of cards, like news articles, that maintain their proportions even when the browser's width changes. To achieve this, I decided to use an aspect-ratio hack by s ...

What's the best way to ensure images and videos in a div maintain their aspect ratio when resizing?

Is there a way to make the image and video fit inside the div without altering the aspect ratio? The code snippet below is what gets displayed on my website, utilizing bootstrap for responsiveness. <div id="micropost-208"> <di ...

When the "send" button is clicked on a particular contact form, it is actually redirecting to a different form instead of sending the

My one-page website originally had a contact form at the end, But then I decided to get creative and copied that form, made some changes, and added it to a pop-up window that appears when clicking on three different buttons in the middle of the page. How ...

Obtain the HTML content of a webpage a few seconds following its initial loading

Currently, I am working on a script in nodejs to automate the process of fetching data from an online directory. Although this is new territory for me, I decided to go with javascript as it is a language I am comfortable with. After some research on Googl ...

React app experiencing an unexpected issue with horizontal scroll bar appearing unnecessarily

Currently, I am attempting to implement a carousel using react-bootstrap within my react application. The goal is to have a black background container where the carousel will reside, similar to this illustration Desired Container However, upon creating t ...

JavaScript doesn't seem to be functioning properly within PHP

I have implemented the supersized jQuery script to incorporate sliding backgrounds on my WordPress page. Now, I aim to create unique slides for different sites and require a PHP if request. This is my code: <?php if ( is_page(array('Restaurant&a ...

Adding space between two Labels with dots in a GridPane layout using JavaFX

Initial Setup: Using a GridPane with 2 Columns Each Column containing a Label https://i.sstatic.net/EkKnl.png Desired Outcome: Filling the space between the labels with dots https://i.sstatic.net/jC7bw.png I've only found String solutions th ...

Challenge involving CSS and Javascript for solving puzzles

In my attempt to create a puzzle with 2 rows and 3 columns using CSS and JavaScript, I envision the pieces of the puzzle being black and cut into various shapes. The objective is for users to drag these pieces onto the board in order to complete it. I have ...

Is it possible for me to create a Rainmeter widget that uses HTML5 geolocation to find and display the nearest town or city?

Currently, I am in the process of creating a Rainmeter widget that will retrieve the user's nearest town and showcase it on the screen. My approach involves utilizing the webparser function along with this specific website. However, despite my best ef ...

Replace the current CSS styles of a pre-installed package

I recently added a package for a type writer effect, but I'm having trouble with it not overriding the CSS styles I've set in the component. Here's an example of what I have: <template> <v-row class="hero" align-content= ...

VueJS displaying broken PNG images after deploying production build

After running the command npm run build, a dist folder is generated containing static assets such as images. The website is using history mode on vue router. Here's the HTML for the first image: https://i.sstatic.net/ZVArw.png Here's a snapsho ...

Pressing the enter key to input password

I have searched through various resources for an answer to my problem, but none of them seem to address it in a way that helps. As a beginner in coding, I may need some extra guidance. Currently, I am only able to submit a password by clicking with the mo ...

Go back to initial value loaded from ajax when canceling the action

On my webpage, I have both a display view (using span) and an edit view (using input) visible to the user. The user can switch between these views using buttons. In the Display View, there is an Edit Button that allows the user to switch to the Edit View. ...