22 posts
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • United States
stevecastaneda says

I’m having a little trouble with this JQuery function.

Basically, I have a div, that when clicked, shows another Div. It’s set to a toggle, actually, so it toggles on/off when you click.

I want to have it where if you click on anywhere outside of the opened div (that appears after you click on the first div), the div that was opened is closed.

I thought I could do this by simply binding a click event to a parent container div, but it seems that the click event carries to all child elements as well, and then the div never opens when you click.

Does that make any sense?

Thanks in advance for any help!

1799 posts
  • Has been a member for 5-6 years
  • Germany
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
BroOf says

Here is a solution I think: Click ME

Explanation: If you klick on a link or a div with the ID ’#link’ it will toggle the div with the ID ’#divLoginBox1’

The return false is for not consulting a anchor or something.

If you click on the same link again the ‘e.stopPropagation();’ will cancel your action.

If you click somewhere else the div will hide.

I hope it helps you!

Greetz: BroOf

22 posts
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • United States
stevecastaneda says

Thank you so much; that works wonderfully! I greatly appreciate you taking the time to write out the code for me.

Also, showing me that IF statement helps me when I don’t want to use toggle(), so I learned something there too.

THANK YOU !

1799 posts
  • Has been a member for 5-6 years
  • Germany
  • Exclusive Author
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 10 and 49 items
  • Referred between 1 and 9 users
BroOf says

Hey no problem. This wasn’t my own code. I’ve found it a few month ago but i think you lernd something :D!

good night!

by
by
by
by
by
by