Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Show Hide Div Using Image  -javascript

Posted on 2011-03-21
10
Medium Priority
?
752 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 11

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 explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
Suggested Courses

916 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