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 7757889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:12:46+00:00 2026-06-01T13:12:46+00:00

I have 4 divs next to each other, each containing a link. I want

  • 0

I have 4 divs next to each other, each containing a link. I want to dynamically switch the id of “current” around,

for example.

<div name="1" id="current">
<a>
link
</a>
</div>
<div name="2">
<a>
link
</a>
</div>
<div name="3">
<a>
link
</a>
</div>
<div name="4">
<a>
link
</a>
</div>

How can I dynamically change which one is the current one when the link is clicked?

  • 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-06-01T13:12:48+00:00Added an answer on June 1, 2026 at 1:12 pm

    Don’t do this. Element IDs should not change based on the state of the web page. Use another attribute instead, like class="current".

    If you’re using jQuery, which I highly recommend, you can do this as follows:

    $('div.tab a').click(function() {
      $('div.tab').removeClass('current');
      $(this).closest('div.tab').addClass('current');
      return false;
    });
    

    To make this code work, you need to add class="tab" to your <div> elements. This is a good idea especially if you have other <div> elements on the page. If you do this, the current tab would then have an attribute class="tab current". jQuery knows how to handle this properly.

    Also, I would consider using the id attribute instead of the name attribute, and giving your <a> tags a href attribute so that browsers will show them as clickable elements. For example:

    <div id="tab1" class="tab">
      <a href="#">tab 1</a>
    </div>
    

    Here’s an example of how to include this script (assuming that you have JQuery saved as a file jquery.js in the same directory as the HTML page):

    <!DOCTYPE html>
    <html>
      <head>
        <title>Testing jQuery</title>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
          $(function() {
            $('div.tab a').click(function() {
              $('div.tab').removeClass('current');
              $(this).closest('div.tab').addClass('current');
              return false;
            });
          });
        </script>
      </head>
      <body>
        <div id="tab1" class="tab current">
          <a href="#">tab1 link</a>
        </div>
        <div id="tab2" class="tab">
          <a href="#">tab2 link</a>
        </div>
        <div id="tab3" class="tab">
          <a href="#">tab3 link</a>
        </div>
        <div id="tab4" class="tab">
          <a href="#">tab4 link</a>
        </div>
      </body>
    </html>
    

    A couple of notes about that:

    • Really, it’s better to put your site’s JavaScript in a separate file and include it with <script type="text/javascript" src="filename.js"></script>, but including it “inline” like I’ve done is fine for small projects or just playing around with stuff.

    • The $(function() { ... }); is shorthand for $(document).ready(function() { ... });, which means the code inside that block will be executed as soon as the browser has finished loading the HTML document structure. For example, you don’t want your JavaScript to execute before the browser has finished downloading the rest of the page.

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

Sidebar

Related Questions

I have 3 divs floating next to each other in a containing div. The
i have 2 divs next to each other i want mouseleave to fire when
I have two div's and I want to show them next to each other.
I have a number of divs next to each other (horizontally) and want the
I have a wrapper div which contans 2 divs next to each other. Above
I want to have 5 divs horizontally next to each other spanning the width
I have two divs floating next to each other, with class name div1 and
I have two floating divs next to each other, and want the left one
i want to position 4 divs next to each other, at a certain position
This is the situation, I have 2 div's next to each other. One div

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.