0

I've read lots of posts on this but I'm still struggling. I'm trying to show and hide a div class using some simple java. I can get it to work in JSFiddle but not on my live site.

Here's the code I'm using. HTML first

<div id="mydiv1">
    <div id="mydiv-container">
        <div id="mydiv-content">
            <h1>Here's The Popup 1</h1>
            <br>Click the link to close.
            <br>
            <a href="#" onclick="show('mydiv2')">Open 2</a>
        </div>
    </div>
</div>

<div id="mydiv2"  style="display:none">
    <div id="mydiv-container">
        <div id="mydiv-content">
            <h1>Here's The Popup 2</h1>
            <br>Click the link to close.
            <br>
            <a href="#" onclick="hide('mydiv2')">Close 2</a>
        </div>
    </div>
</div>

JavaScript:

function show(target) {
document.getElementById(target).style.display = 'block';
}

function hide(target) {
document.getElementById(target).style.display = 'none';
}

Can't workout why it wont switch divs on live server.

5
  • where did you defined the functions?? Commented May 15, 2015 at 11:51
  • 4
    Did you check if ids mydiv1/mydiv2 are unique Commented May 15, 2015 at 11:52
  • this code is working fine check you id (mydiv2) is not repeating. Commented May 15, 2015 at 11:54
  • Just make sure the functions are known before assigning them. if I change from including the JS in the <head> to onLoad, it will say the functions aren't defined. jsfiddle.net/svArtist/x9yh3yr9 Commented May 15, 2015 at 11:56
  • add return false after onclick, ie, onclick="show('mydiv2'); return false" and onclick="hide('mydiv2') return false." Commented May 15, 2015 at 12:25

1 Answer 1

1

I think the problem on your server is the ids i.e. mydiv1 and mydiv2 are not unique. The ids are repeating.

Check on your server if ids are not duplicating.

id should always be unique.

You can use Web Developer extension on Firefox and Chrome. To get duplicate ids on page

  1. Install Web Developer on your browser
  2. Open options by clicking on Web Developer icon
  3. Click on Information tab
  4. Click on Find Duplicate Ids

Hope this will help.

To Download toolbar:

Chrome: https://chrome.google.com/webstore/detail/web-developer/bfbameneiokkgbdmiekhjnmfkcnldhhm?utm_source=chrome-app-launcher-info-dialog

Firefox: https://addons.mozilla.org/en-US/firefox/addon/web-developer/?src=userprofile

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, thanks for the advice. Did as you suggested and the id's are repeating. How do I prevent this?
Don't repeat in your code. :). Change ids of these elements
Me being dumb - I've now removed duplicate id's but still having problems showing second div. The url for this site is www.picassosparadise.com
Hmmm, If I view the JS file in web developer it shows a html file!
Okay, fixed the error with the js file not loading, that was a dodgy url .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.