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

  • SEARCH
  • Home
  • 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 9241271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:20:29+00:00 2026-06-18T08:20:29+00:00

When i click on a link, i want a div to become visible. However,

  • 0

When i click on a link, i want a div to become visible. However, i need to do this by knowing which link is clicked on, by checking which id it has. The content is hidden, but will not become visible when i click on a link. I am not allowed to use jquery. Any solution?

Javascript

function show() {
    var a = document.getElementsByTagName("a");
    if (a.id == "link1") {
        document.getElementByID("content1").style.visibility = 'visible';
    } else if (a.id == "link2") {
        document.getElementByID("content2").style.visibility = 'visible';
    } else if (a.id == "link3") {
        document.getElementByID("content3").style.visibility = 'visible';
    } else if (a.id == "link4") {
        document.getElementByID("content4").style.visibility = 'visible';
    }
}

function init() {

    var divs = document.getElementsByTagName("div");
    for (i = 0; i < divs.length; i++) {
        if (divs[i].className == "div") {
            divs[i].style.visibility = 'hidden';
        }
    }
    var a = document.getElementsByTagName("a");
    a.onclick = show;
}

window.onload = init;

HTML

<head>
    <title>Inl1-1</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="stylesheet" type="text/css" href="style-1.css">
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="Uppg1.js"></script>
</head>
<body>
    <ul class="meny" id="menu">
        <li><a href="#" id="link1">Utvärdering/Feedback</a>
        </li>
        <li><a href="#" id="link2">Kontakt</a>
        </li>
        <li><a href="#" id="link3">Öppettider</a>
        </li>
        <li><a href="#" id="link4">Om Asperöd</a>
        </li>
    </ul>
    <div class="div" id="content1">
        <p>Du kan kontakta oss på följande nummer:
            <br/>040-123456</p>
        <p>Du kan även mejla oss:
            <br/>aperöd@hotmail.com</p>
    </div>
    <div class="div" id="content2">
        <p><b>Asperåd Äventyrsland</b>
        </p>
        <p>Växel: 0200-123456999 (kl.08:30-15)</p>
        <p>Stora Vägen 140</p>
        <p>289 22 Aperöd</p>
        <p>Skicka oss din <a href="Inl1-3.html">fråga</a>
        </p>
    </div>
    <div class="div" id="content3">
        <p>Vi har följande öppettider:</p>
        <p>Mån-Fre: 10:00 - 20:00</p>
        <p>Lör: 10:00 - 18:00</p>
    </div>
    <div class="div" id="content4">
        <p>Asperöd är en fin park för alla möjliga personer. Vi erbjuder en massa,
            <br/>men det kostar 500kr för att delta för en dag.</p>
        <p>Asperöd är fylld med turister som besöker platsen minst en gång per månad.</p>
        <p>Asperöd är bland de största nöjesparkerna i Scandinavien.</p>
    </div>
</body>
  • 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-18T08:20:30+00:00Added an answer on June 18, 2026 at 8:20 am

    Create a links object which provides the mappings. Grab the target element from the event and store its id. Then use the id to grab the proper element’s id from the links object.

    function show(event){
    var links = {
        link1: "content1",
        link2: "content2",
        link3: "content3",
        link4: "content4"
    };
    
    for(var x = 0; x < links.length; x++){
       document.getElementById(links[id]).style.visibility = "hidden";
    }
    
    var id = event.target.id;
    var a = document.getElementsByTagName("a");
    document.getElementById(links[id]).style.visibility = 'visible';
    }
    

    There is also an issue with the init function. Since a is an array of elements you must iterate this array and bind the show function to the onclick event.

    function init() {
    
        var divs = document.getElementsByTagName("div");
        for (i = 0; i < divs.length; i++) {
            if (divs[i].className == "div") {
                divs[i].style.visibility = 'hidden';
            }
        }
        var a = document.getElementsByTagName("a");
        for(var x = 0; x < a.length; x++){
           a[x].onclick = show;
        }
    }
    

    Working Example: http://jsfiddle.net/538s2/

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

Sidebar

Related Questions

I want to change my div content on the click of a link, this
I want to do something like $('a.filter-link').click(function(e) { e.preventDefault(); var that = $(this), categoryId
I want to make a code that counts the click on the link which
I need somethings like this : when i click on some link, the whole
I want to show a div on link click and hide if click outside
i have a div tag that has a link in it. I want to
I want to click a link and toggle between 2 blocks of content, within
I have index page,which contain div section.i want to call different pages into this
I want to toggle element that id's pg when click link. header: <title>Example</title> <script
Here is what I want to happen: The user should click a link/button that

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.