6 Replies - 626 Views - Last Post: 28 January 2016 - 09:47 AM

#1 Desertt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 182
  • Joined: 27-October 13

e.target conflict with toggle function

Posted 22 January 2016 - 07:14 AM

Hi i'm using those function to open div on a button and if clicked outside the div it close
$("#RightMenu").click(function() {
	var img1 = '<img src="./MediumImages/Right-Menu-Icon_IPad.png">';
	var img2 = '<img src="./MediumImages/Close-Icon_IPad.png">';
	var img =  $(this).html();
	if(img == img1) {
		$(this).html(img2);
		$("#menu_hidden").show();
	}
	else {
		$(this).html(img1);
		$("#menu_hidden").hide();
	}
	$(".left_hidden_menu").hide();
	$("#LeftMenu").html('<img src="./MediumImages/Left-Menu-Icon_IPad.png">');
});
$("#LeftMenu").click(function() {
	var img1 = '<img src="./MediumImages/Left-Menu-Icon_IPad.png">';
	var img2 = '<img src="./MediumImages/Close-Icon_IPad.png">';
	var img =  $(this).html();
	if(img == img1) {
		$(this).html(img2);
		$(".left_hidden_menu").show();
	}
	else {
		$(this).html(img1);
		$(".left_hidden_menu").hide();
	}
	$("#menu_hidden").hide();
	$("#RightMenu").html('<img src="./MediumImages/Right-Menu-Icon_IPad.png">');
});
$(document).mouseup(function (e) {
	var container = $("#menu_hidden");
	
	if (!container.is(e.target) // if the target of the click isn't the container...
	  && container.has(e.target).length === 0) // ... nor a descendant of the container
	{
			container.hide();
	}
	
	var container1 = $(".left_hidden_menu");
	
	if (!container1.is(e.target) // if the target of the click isn't the container...
	  && container1.has(e.target).length === 0) // ... nor a descendant of the container
	{
		container1.hide();
	}
});


I'm trying to change the image from
if (!container.is(e.target) // if the target of the click isn't the container...
	  && container.has(e.target).length === 0) // ... nor a descendant of the container
	{
                        var img = $("#RightMenu").html();
                        if(img == img1) {
		        $(this).html(img2);
			container.hide();
	}


But when i added this code the container always stay closed , I've used a code for the opening
$("#RightMenu").click(function() {
	var img1 = '<img src="./MediumImages/Right-Menu-Icon_IPad.png">';
	var img2 = '<img src="./MediumImages/Close-Icon_IPad.png">';
	var img =  $(this).html();
	if(img == img1) {
		$(this).html(img2);
	else
		$(this).html(img1);
        $("#menu_hidden").toggle;
	$(".left_hidden_menu").hide();
	$("#LeftMenu").html('<img src="./MediumImages/Left-Menu-Icon_IPad.png">');
});


but the toggle function stoped working is there any solution ?

Is This A Good Question/Topic? 0
  • +

Replies To: e.target conflict with toggle function

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: e.target conflict with toggle function

Posted 22 January 2016 - 07:18 AM

Are there errors in your browser's console?
Was This Post Helpful? -1
  • +
  • -

#3 Desertt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 182
  • Joined: 27-October 13

Re: e.target conflict with toggle function

Posted 23 January 2016 - 02:26 AM

No errors at all , i fired some alerts and i worked also correctly but as i mention the result wont show as i expected, second why the .toggle() not working if i added the .mouseup function ?
Was This Post Helpful? 0
  • +
  • -

#4 Desertt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 182
  • Joined: 27-October 13

Re: e.target conflict with toggle function

Posted 25 January 2016 - 08:05 AM

Any other suggestions ?
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: e.target conflict with toggle function

Posted 25 January 2016 - 09:40 AM

The code you posted has a syntax error, either you're not checking for errors or you're not posting the right code. What code are you using now?
Was This Post Helpful? 0
  • +
  • -

#6 Desertt   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 182
  • Joined: 27-October 13

Re: e.target conflict with toggle function

Posted 28 January 2016 - 12:04 AM

This is the code that i'm using right now

<script>
$("#RightMenu").click(function() {
	var img1 = '<img src="./MediumImages/Right-Menu-Icon_IPad.png">';
	var img2 = '<img src="./MediumImages/Close-Icon_IPad.png">';
	var img =  $(this).html();
	if(img == img1) {
		$(this).html(img2);
		$("#menu_hidden").show();
	}
	else {
		$(this).html(img1);
		$("#menu_hidden").hide();
	}
	$(".left_hidden_menu").hide();
	$("#LeftMenu").html('<img src="./MediumImages/Left-Menu-Icon_IPad.png">');
});
$("#LeftMenu").click(function() {
	var img1 = '<img src="./MediumImages/Left-Menu-Icon_IPad.png">';
	var img2 = '<img src="./MediumImages/Close-Icon_IPad.png">';
	var img =  $(this).html();
	if(img == img1) {
		$(this).html(img2);
		$(".left_hidden_menu").show();
	}
	else {
		$(this).html(img1);
		$(".left_hidden_menu").hide();
	}
	$("#menu_hidden").hide();
	$("#RightMenu").html('<img src="./MediumImages/Right-Menu-Icon_IPad.png">');
});
$(document).mouseup(function (e) {
	var container = $("#menu_hidden");
	var container1 = $(".left_hidden_menu");
	
	if (!container.is(e.target) // if the target of the click isn't the container...
	  && container.has(e.target).length === 0) // ... nor a descendant of the container
	{
			container.hide();
	}
	
	if (!container1.is(e.target) // if the target of the click isn't the container...
	  && container1.has(e.target).length === 0) // ... nor a descendant of the container
	{
		container1.hide();
	}
});
</script>


This post has been edited by Desertt: 28 January 2016 - 12:04 AM

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: e.target conflict with toggle function

Posted 28 January 2016 - 09:47 AM

It's going to run both of those event handlers when you click (it will run one for the element you clicked on, and then the mouseup handler). Add some console.log statements into those functions to figure out what is going on. It's probably immediately hiding the element that it just showed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1