Jump to content

Add an Remove Class Kills JavaScript


unemployment

Recommended Posts

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.

 

                        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;
				}
			};

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.