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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:57:27+00:00 2026-06-06T10:57:27+00:00

I am new to JavaScript and actually quite desperate by now I have an

  • 0

I am new to JavaScript and actually quite desperate by now

I have an HTML file that:

  1. gets data from an XML file and displays them in various divs (e.g. )
  2. these divs are hidden (by default) by a class name (class=’box’)
  3. when a link is clicked, I pass the ‘href’ to the function showContent, remove the #, and then look for an element with that ID in the document.
  4. then I add a new class name (‘show’) – so that this element shows up!

If you run the code you will see that every time you click on a link a new div is displayed…

So current problems:

  1. replace already shown divs with the new clicked ID so that only one div shows up every time.
  2. How can I avoid inserting the onClick event in every single tag – and make this more automated?

My code is as follows:

function showContent(obj)
{
var linkTo = obj.getAttribute("href");
var newlinkTo=linkTo.replace('#','');
//alert (newlinkTo);

document.getElementById(newlinkTo).innerHTML=" This is where the xml variable content should go";   
document.getElementById(newlinkTo).className += " Show";

return true;
}

<a href="#b0" onClick="return showContent(this);">
<div id="text_content"> link2 </div>
</a>

<a href="#b1" onClick="return showContent(this);">
<div id="text_content"> link 1 </div>
</a>

<div class='box' id='b0'> abstract content </div>
<div class='box' id='b1'> introduction content </div>
  • 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-06T10:57:28+00:00Added an answer on June 6, 2026 at 10:57 am

    This sort of thing is not hard to do without jQuery.

    I would recommend using a hash-bang (#!) for Javascript activated links to keep it separate from other possible links with hashes. (script is at the bottom)

    <div id="nav-links">
    
        <a href="#!b0">
        <div id="text_content"> link2 </div>
        </a>
    
        <a href="#!b1">
        <div id="text_content"> link 1 </div>
        </a>
    
    </div>
    
    <div class='box' id='b0'> abstract content </div>
    <div class='box' id='b1'> introduction content </div>
    
    <script type="text/javascript">
    
        var links = document.getElementById('nav-links').getElementsByTagName('a');
        for(var i = 0, link; link = links[i]; i++) {
            link.onclick = showContent;
            // Hide content divs by default
            getContentDiv(link).style.display = 'none';
        }
        // Show the first content div
        if(links.length > 0) showContent.apply(links[0]);
    
        var current;
    
        function showContent() {
    
            // hide old content
            if(current) current.style.display = 'none';
    
            current = getContentDiv(this);
            if(!current) return true;
    
            //current.innerHTML = "This is where the xml variable content should go";
            current.style.display = 'block';
    
            return true;
    
        }
    
        function getContentDiv(link) {
    
            var linkTo = link.getAttribute('href');
    
            // Make sure the link is meant to go to a div
            if(linkTo.substring(0, 2) != '#!') return;
            linkTo = linkTo.substring(2);
    
            return document.getElementById(linkTo);
    
        }
    
    </script>​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new to javascript but have undertaken a task to get better aquainted
Im quite new at Javascript however I have been trying to create a currency
So I'm pretty new to javascript...right now I'm just writing that dynamic logout button
new with javascript here. I have a simple form that takes what the user
I'm new to JavaScript and jQuery. I want to click a button and have
I'm trying to improve my Javascript coding style and have been reading that it's
Firstly, the Google Chart API example from Google (it is javascript) actually <script type=text/javascript>
I am new to javascript, so excuse my lack of actual code. I have
I have a lock in my c# web app that prevents users from running
I'm actually studying Crockford's Javascript: the good parts . I am new to JavaScript

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.