Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Show Hide Div Using Image  -javascript

Posted on 2011-03-21
10
Medium Priority
?
746 Views
Last Modified: 2012-08-14
I am trying to follow a demonstration online, which allows you to click on an image, and it will close a div which contails some text.

I want to modify this though,  I want the div to be closed when opening the page, and when you click the button image, the div will open.

I am using this in wordpress, and have a gravity form which i want to show, once somebody clicks the button.  But i cannot seem to get this to work. It correctly closes the div containing the form, but appears open when you visit the page for the first time.

Here is the code i am using.

 
// JavaScript Document
function toggle5(showHideDiv, switchImgTag) {
        var ele = document.getElementById(showHideDiv);
        var imageEle = document.getElementById(switchImgTag);
        if(ele.style.display == "block") {
                ele.style.display = "none";
		imageEle.innerHTML = '<img src="http://localhost/drughelpdesk/wp-includes/images/plus.png">';
        }
        else {
                ele.style.display = "block";
                imageEle.innerHTML = '<img src="http://localhost/drughelpdesk/wp-includes/images/minus.png">';
        }
}

Open in new window

<div id="headerDivImg">
    <a id="imageDivLink" href="javascript:toggle5('contentDivImg', 'imageDivLink');"><img src="http://localhost/drughelpdesk/wp-includes/images/minus.png"></a>
</div>
<div id="contentDivImg" style="display: block;">[gravityform id=7 name=10Panel Drug Testing Kit With Lab Confirmation title=false ajax=true]</div>

Open in new window


I'm following the "let's uses images demo" situated here
0
Comment
Question by:Jon Imms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 1

Expert Comment

by:GamesNET2
ID: 35184891
Look up the .toggle attribute using jquery - here is a link, this should sort you problem. http://api.jquery.com/toggle/
0
 

Author Comment

by:Jon Imms
ID: 35184956
Sorry, That did not help,  People are asking the same question as me on that page, and it has not been answered in 2 months.  All those querys on the page, all show the contents of the div first, and then clicking on the image closes it. I want the div to be closed by default, and clicking the image opens the div which contains my form.
0
 
LVL 1

Expert Comment

by:GamesNET2
ID: 35185001
Ah ok, set the display: none; in the css for the div, that means it will load hidden. Then using the toggle jquery you can then show it,

so

.yourdivclass {display: none;}

$(function() {
$(".yourdivclass").click(function() {$(this).Toggle();});
});

Something like that - (that might be slightly wrong but you get the idea....
0
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 

Author Comment

by:Jon Imms
ID: 35185114
Still cannot do it :(     It keeps on showing the Image, and the div containing my form by default. Clicking the image hides the div containing the form.
0
 
LVL 10

Accepted Solution

by:
Jeffrey Dake earned 2000 total points
ID: 35185974
If I understand correctly you just want to switch the div to be closed by default.  Just change the div to start with display:none

<div id="headerDivImg">
    <a id="imageDivLink" href="javascript:toggle5('contentDivImg', 'imageDivLink');"><img src="http://localhost/drughelpdesk/wp-includes/images/minus.png"></a>
</div>
<div id="contentDivImg" style="display: none;">[gravityform id=7 name=10Panel Drug Testing Kit With Lab Confirmation title=false ajax=true]</div>

Open in new window

0
 

Author Closing Comment

by:Jon Imms
ID: 35186043
Perfect. Just what i needed thank you.
0
 
LVL 1

Expert Comment

by:GamesNET2
ID: 35186205
Am I going mad - is that not what I said?!
0
 

Author Comment

by:Jon Imms
ID: 35190051
No, it's not what you said.

You said
"something like that", " might be wrong"
. That did not help me.
0
 
LVL 1

Expert Comment

by:GamesNET2
ID: 35190120
Ah I thought display: none fixed it. oh well.
0
 

Author Comment

by:Jon Imms
ID: 35190147
I did try your solution but it did not work for me,  The solution the other guy gave was exactly what i needed.  Thank you for your help though.  Not having a go at you or anything :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question