/    Sign up×
Community /Pin to ProfileBookmark

Second onClick not working

I have a page with a div that is hidden until clicked on, and can be hidden if clicked on again (at least that is the goal). The html looks like:

[code=html]
<div id=’search_box’ class=’div_hide’ onClick=’toggle_div(this.id);’>
<h2><img class=’arrow_img’ src=’image.png’ />Text</h2>
<span>Content (search box)</span>
</div>
[/code]

I currently have the following javascript:

[CODE]
function toggle_div (divId) {
var divName = document.getElementById(divId);
var divStatus = divName.className;
var divImage = divName.getElementsByTagName(‘img’);
var imgExpand = ‘image1.png’
var imgCollapse = ‘image2.png’
if (divStatus = ‘div_show’) {
divStatus = divStatus.replace(/bdiv_showb/,”);
divName.className = divStatus;
newStatus = ‘div_hide’;
divStatus.className = newStatus;
divImage.src = imgExpand;
}
else {
divStatus = divStatus.replace(/bdiv_hideb/,”);
divName.className = divStatus;
newStatus = ‘div_show’;
divStatus.className = newStatus;
divImage.src = imgCollapse;
*/
}
}
[/CODE]

My goal being to click on the main <div> and show the content in the <span> and change the image, and then clicking again returns it to it’s original (hidden) state. I have been able to successfully show the <div> but can’t get it to hide again. And when troubleshooting it using Firebug it shows that clicking on the <div> is simply removing the class that hides it, leaving no class, and definately not replacing it like I am trying to do. Any ideas why the second click is not producing ANY results? Also, if I get it working I have thought that by making the entire <div> have the onClick handler if someone tries to click inside the unhidden <span> (for links or to click in a search box) would result in hiding the <span> rather than following the link or focusing on the input box. Is there any possibility for that?

The main reason handled the <div> is because there will be multiple sections that I would like to reuse the function on, and creating an id for the <div> and then referencing this.id was the first way I was able to get the functionality that I currently have. So any suggestions for improvement are appreciated ?

Thanks!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@BIOSTALLApr 23.2010 — Try changing:

[CODE]if (divStatus = 'div_show') {[/CODE]

To:

[CODE]if (divStatus == 'div_show') {[/CODE]

What your doing at the moment is assigning 'divStatus' to 'div_show' every time.. hence the second click is not working.

Hope that helps
Copy linkTweet thisAlerts:
@AquaChaosauthorApr 23.2010 — Thanks for the advice Biostall. Unfortunately that didn't help.

Did some more fiddling and ended up with this:
[code=html]
<div>
<h2 id='sbox_header' onClick='toggle_div(this.id, "sbox_open");'><img class='arrow_img' src='myimage1.png' />$lang[heading]</h2>
<span id='sbox_open' style='display: none;'>
<input type="text" size="15" name="s[search]" /><input type="submit" value="Find" />
</span>
</div>
[/code]

[CODE]
function toggle_div (mainDiv, hiddenDiv) {
if (document.getElementById(hiddenDiv).style.display == 'none') {
document.getElementById(hiddenDiv).style.display = 'inline';
document.getElementById(mainDiv).getElementsByTagName("img")[0].src='myimage1.png';
}

else {
document.getElementById(hiddenDiv).style.display = 'none'
document.getElementById(mainDiv).getElementsByTagName("img")[0].src='myimage2.png';
}
[/CODE]


I think this will work a little better for me, as the H2 will be always visable, and the hidden part may vary between <span>'s, <div>'s, <ul>'s. Ends up being a little more HTML, but less code.

If anyone can shed some light on why my previous attempt failed I would be curious to know. Also with the images I tried creating a 3rd variable that was entered into the call of onClick and tried
[CODE]
document.getElementById(imgName).src="myimage.png";
[/CODE]


but had no results. I also tried

[CODE]
document[imgName].src="myimage.png'
[/CODE]


and had success, but the new way works better (as I don't have to assign an ID or name to it), but just wondering for future reference.
×

Success!

Help @AquaChaos spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...