• 0

[Javascript] Open / Close Div help needed


Question

Hey all,

I am currently implementing this http://javascriptkit.com/script/script2/dropdownpanel.shtml into a new site im doing but am having a tiny niggle with it. the code will open a little dropdown div from the top of the page and works fantastically well but..... it closes on any click to the whole div.

I am looking to add a form into the dropdown but am unable to do so as any click within the area closes the dropdown.

Can anybody shed some light on how to stop this from happening and simply have it close when the button is clicked instead?

Thanks in advance

11 answers to this question

Recommended Posts

  • 0

The script is designed to toggle on click, although it should only be for the panel itself, and not its containing elements, so you should be able to click a link or a button. You could modify jkpanel.js to split the open/close functionality; perhaps open to onmouseover and close to onmouseout, or some variation of mouse events.

  • 0

Hello, Change the jkpanel.js file to this and it will be how you want it. Do have a good one. Send use a link to your site when you have completed it.

//Drop Down Panel script (March 29th, 08'): By JavaScript Kit: http://www.javascriptkit.com

var jkpanel={
	controltext: 'Panel Content',
	$mainpanel: null, contentdivheight: 0,
	$controlstick: null,

	openclose:function($, speed){
		this.$mainpanel.stop() //stop any animation
		if (this.$mainpanel.attr('openstate')=='closedmainpanel.animate({top: 0}, speed).attr({openstate: 'open'})
		else
			this.$mainpanel.animate({top: -this.contentdivheight+'px'}, speed).attr({openstate: 'closed'})
	},

	init:function(file, height, speed){
		jQuery(document).ready(function($){
			jkpanel.$mainpanel=$('<div id="dropdownpanel"><div class="contentdiv"></div><div class="control">'+jkpanel.controltext+'</div></div>').prependTo('body')
			var $contentdiv=jkpanel.$mainpanel.find('.contentdiv		jkpanel.$controlstick=jkpanel.$mainpanel.find('.control		var $controldiv=jkpanel.$mainpanel.find('.control').css({cursor})
			$contentdiv.load(file, '', function($){
					var heightattr=isNaN(parseInt(height))? 'auto' : parseInt(height)+'px'
					$contentdiv.css({height: heightattr})
					jkpanel.contentdivheight=parseInt($contentdiv.get(0).offsetHeight)
					jkpanel.$mainpanel.css({top:-jkpanel.contentdivheight+'pxisibility:'visible'}).attr('openstate', 'closed')
					$controldiv.css({cursor:'handursor:'pointer'})
			})
			jkpanel.$controlstick.click(function(){jkpanel.openclose($, speed)})
		})
	}
}

//Initialize script: jkpanel.init('path_to_content_fileheight of content DIV in px', animation_duration)
jkpanel.init('panelcontent.htm200px', 500)

  • 0

The way above seems a bit OTT to me.

In the original jkpanel.js simply replace

jkpanel.$mainpanel.click(function(){jkpanel.openclose($, speed)})

with

$controldiv.click(function(){jkpanel.openclosepanel($, speed); return})

  • 0

I am also using jkpanel and I am very happy with the results so far.

However, I am looking for some help - I want to set it so that wherever I click on the page, it will close the current panel. For example, the user opens the panel, but to hide it they can click anywhere on the page, rather than having to click inside the panel.

I hope someone out there can help me! Many thanks,

Si

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • In addition to the political nature of Elon musk aside. AI Chat bots still have issues that is still a bit of a black box they are still trying to figure and are still not trust worthy to do what they say at this time. This is an area of “go fast and break things” mantra with tech is a very bad idea.
    • Electron sucks. I was much happier when Teams moved off it.
    • iOS 27 will focus on features for Apple's first foldable phone coming 2026 by Taras Buria Foldable smartphones are hardly a novel thing anymore. The form factor has been around for a while, and Samsung, the pioneer of the idea, is already at the seventh generation. Now, with the concept and tech being plenty mature, Apple is joining the party with its first foldable, rumored to arrive in 2026. According to Mark Gurman from Bloomberg, Apple's next iOS 27 update will prioritize new features for the rumored foldable iPhone. The development of iOS 27 is starting soon, and Apple wants to put a lot of focus on the folding experience. With the first foldable iPhone, Apple is entering a well-developed market, and the company is not expected to bring major innovations, unlike the original Apple Vision Pro. Apple is reportedly focusing on a design similar to the Galaxy Z Fold7 with an inward-folding display. Still, the first iPhone is rumored to feature some notable improvements for the inner display crease and the hinge. Foldable phones are becoming increasingly popular in China, and Apple can no longer ignore the market. Offering an expensive foldable iPhone will help the company boost revenue and reignite interest in its main product. There are plenty of users who are interested in giving a book-like foldable a try, but having Android as the only platform choice is a big turn-off for many. With Apple joining the party, the form factor will become much more interesting to many users. Still, those users will have to make sure their pockets are plenty deep. The first foldable iPhone is not expected to cost less than $2,000, so the sticker shock of the original iPhone X from 2017 and Apple Vision Pro from 2023 is coming next year. Source: Bloomberg
  • Recent Achievements

    • Rookie
      Snake Doc went up a rank
      Rookie
    • First Post
      nobody9 earned a badge
      First Post
    • One Month Later
      Ricky Chan earned a badge
      One Month Later
    • First Post
      leoniDAM earned a badge
      First Post
    • Reacting Well
      Ian_ earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      Michael Scrip
      205
    3. 3
      ATLien_0
      201
    4. 4
      Xenon
      138
    5. 5
      +FloatingFatMan
      117
  • Tell a friend

    Love Neowin? Tell a friend!