/    Sign up×
Community /Pin to ProfileBookmark

Add an Remove Class Kills JavaScript

I am in the process of making a drop down menu. When the toggle button is clicked, the menu is displayed and when you click away from the menu, the menu disappears and when you click the menu, your menu item is selected and the menu disappears, but… I had one small problem. When I have the menu open, I was able to click another menu and have it displayed. I didn’t want more than one menu displayed at a time. As a result, I added in another for loop to my onclick event that will search for all the divs that have a menu and it will close them which works, but now if I click on a menu, the menu won’t close. For some my commented out remove and add class functions are killing my list.onclick = function(). I have no idea why.

[CODE]
allDivs[i].onclick = function()
{
var divIdFull = document.getElementById(this.id);
var divId = this.id.substr(12);
var img = this.getElementsByTagName(‘img’);
var img = img[0];
var link = document.getElementById(‘goal_icon_’ + divId);
var item = document.getElementById(‘goal_item_’ + divId);
var list = document.getElementById(‘identifier_’ + divId);
var uncompleted = document.getElementById(‘uncompleted_’ + divId);
var on_hold = document.getElementById(‘on_hold_’ + divId);
var cancelled = document.getElementById(‘cancelled_’ + divId);
var completed = document.getElementById(‘completed_’ + divId);
var status_input = document.getElementById(‘goal_status_’ + divId);
var list2 = list.getElementsByTagName(‘li’);
var list_length = list2.length;

list.onclick = function()
{
removeClass(divIdFull, ‘hover’);
removeClass(list, ‘block’);
}

var allDivs2 = document.getElementsByTagName(‘div’);
for (var i=0; i< allDivs2.length; i++)
{
if (allDivs2[i].className.indexOf(‘goal_icon_holder’) != -1)
{
var newdivid = allDivs2[i].id.substr(12);
var link_r = document.getElementById(‘goal_icon_’ + newdivid );
var list_r = document.getElementById(‘identifier_’ + newdivid );

//removeClass(link_r, ‘hover’);
//removeClass(list_r, ‘block’);
//addClass(list_r, ‘dn’);
}
}

if (link.className == ‘goal_icon_button’)
{
addClass(link, ‘hover’);
removeClass(list, ‘dn’);
addClass(list, ‘block’);
}
else
{
removeClass(link, ‘hover’);
removeClass(list, ‘block’);
addClass(list, ‘dn’);
}

function clickHandler(node)
{
for(var i=0;i<list_length;i++)
{
removeClass(list2[i], ‘click’);
}

return true;
}

uncompleted.onclick = function()
{
clickHandler();
toggleClass(this.parentNode, ‘click’);
img.src = “/assets/img/icons/process.png”;
status_input.value = 0;
removeClass(divIdFull, ‘hover’);
removeClass(list, ‘block’);
return false;
}

on_hold.onclick = function()
{
clickHandler();
toggleClass(this.parentNode, ‘click’);
img.src = “/assets/img/icons/process_warning.png”;
status_input.value = 1;
return false;
}

cancelled.onclick = function()
{
clickHandler();
toggleClass(this.parentNode, ‘click’);
img.src = “/assets/img/icons/process_remove.png”;
status_input.value = 2;
return false;
}

completed.onclick = function()
{
clickHandler();
toggleClass(this.parentNode, ‘click’);
img.src = “/assets/img/icons/process_accept.png”;
status_input.value = 3;
return false;
}

return false;
}

var closemenu = null;

// for each appropriate div {
allDivs[i].onmouseover = function() { closemenu = null; };
allDivs[i].onmouseout = function()
{
closemenu = this;
};
// }

document.onclick = function() {
if (closemenu != null)
{
var allDivs = document.getElementsByTagName(‘div’);
for (var i=0; i< allDivs.length; i++)
{
if (allDivs[i].className.indexOf(‘goal_icon_holder’) != -1)
{
var divIdFull = document.getElementById(allDivs[i].id);
var divId = allDivs[i].id.substr(12);
var link = document.getElementById(‘goal_icon_’ + divId);
var list = document.getElementById(‘identifier_’ + divId);

removeClass(link, ‘hover’);
removeClass(list, ‘block’);
addClass(list, ‘dn’);
}
}

// closemenu is the <div> to hide
closemenu = null;
}
};
[/CODE]

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @unemployment 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 5.18,
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,
)...