Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 693159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:43:30+00:00 2026-05-14T02:43:30+00:00

This is a two-part question. I’m using jQuery for a project and wanting to

  • 0

This is a two-part question. I’m using jQuery for a project and wanting to click a link and toggle the class name “highlight” to that link and also to the div with the same id as the rel attribute of the link. I then want to be able to link to the next div without the classname of “highlight”. Here’s the HTML for it:

<ul>
   <li><a href="#" rel="panel1">Item 1</a></li>
   <li><a href="#" rel="panel2">Item 2</a></li>
   <li><a href="#" rel="panel3">Item 3</a></li>
</ul>  

<a href="">go to next div without class of highlight</a>


<div id="panel1">some text</div>
<div id="panel2">some text</div>
<div id="panel3">some text</div>

Can anyone help with jQuery side of things?

Many thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:43:30+00:00Added an answer on May 14, 2026 at 2:43 am

    Assuming HTML like this:

    <ul>
      <li><a href="#" rel="panel1">Item 1</a></li>
      <li><a href="#" rel="panel2">Item 2</a></li>
      <li><a href="#" rel="panel3">Item 3</a></li>
    </ul>
    
    <a id="next-unhighlighted">Go to next div without class "highlight"</a>
    
    <div class="panel" id="panel1">Panel 1</div> <!-- Note the added -->
    <div class="panel" id="panel2">Panel 2</div> <!-- "panel" classes -->
    <div class="panel" id="panel3">Panel 3</div>
    

    You can use JS like this:

    $('ul li a').click(function () {
      var $this = $(this), // <a>
          id = $this.attr('rel'),
          nextUnhighlighted = $('#next-unhighlighted'), // <a>
          targetDiv = $('#' + id),
          nextDiv;
    
      // Un/highlight the clicked link
      $this.toggleClass('highlight');
    
      // Un/highlight the div related to the target link
      targetDiv.toggleClass('highlight');
    
      // Update nextUnhighlighted to point to next unhighlighted div
      nextDiv = $('div.panel:not(.highlight)');
      if (nextDiv[0]) { // A next sibling was found
        nextUnhighlighted.attr('href', '#' + nextDiv.attr('id'));
      } else {
        nextUnhighlighted.removeAttr('href');
      }
    });
    

    ​
    Note that, if the final panel is already highlighted, then this code does not update the href attribute for a#next-unhighlighted, but removes it. It’s a trivial exercise to add wrap-around behavior, such that highlighting the final panel would link back to the first panel.

    A note about the odd syntax if (nextDiv[0]): If the first element in the jQuery collection nextDiv exists, then there is at least one element in the collection. This behaves similarly to (but not exactly the same as) nextDiv.length > 0, but is marginally faster and smaller.


    As discussed in the comments, to link each panel to the next unhighlighted one, add <a rel="next-panel">Next panel</a> to each panel’s HTML, then add something like this to the main click handler:

    $('div.panel a[rel="next-panel"]').each(function () {
      var $this = $(this),
          nextPanel = $this.parent().next('div.panel:not(.highlight)');
      if (nextPanel[0]) {
        $this.attr('href', '#' + nextPanel.attr('id'));
      }
    });
    

    Depending on your project requirements, you’ll need to initialize each of these next-panel links (or else they’ll only initialize after the first click), and you may want to make the final panel’s ;oml wrap around to the first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a two part question: I am working on a big project where
This is a two part question. A dumb technical query and a broader query
This is actually a two part question. First,does the HttpContext.Current correspond to the current
This is a two part question. Is it possible to take advantage of xVal
This is a two-part question: First, I am interested to know what the best
This is a two part question; firstly, does anyone out there have some insight
This is a two-part question: 1. The original .NET print classes (in System.Drawing.Printing) are
This is a two part question. The first is a specific question about DataSets
This is a two part question from a WPF animation newbie. First, here is
This is a two part question. Q1: Can cURL based request 100% imitate a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.