Webmaster Forum


Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Coding Forum Problems with your code? Let's hear about it.

Lionsanime Directory   World's Leading Outsourcing Network   V7N Directory

Reply
 
LinkBack Thread Tools Display Modes
Old 11-30-2007, 07:10 PM   #1 (permalink)
Inactive
 
Join Date: 11-30-07
Posts: 1
iTrader: 0 / 0%
Latest Blog:
None

mrjokers is liked by many
Javascript Help

Im new to javascript but I found some code on the internet that allows me to display a hidden <div> when a link is clicked on. The <div> disappears when any other part of the page is clicked on. It is similar to google.com's "more" link at the top of the page.

My question is that when I click on the link the div does not disappear. however, when any other part of the page is clicked, it does. How do I make it so that the div disappears when the link is clicked?

Here is my current code:

<html>
<head>
<title>Hover</title>

<style type="text/css">
.dialog {
position:absolute;
padding: 3px;
border: 1px solid gray;
font: 10pt arial;
display: none;
}
</style>

<script type="text/javascript">
function show(cur,element) {
var el = document.getElementById(element);
el.style.left = cur.offsetLeft+"px";
el.style.top = cur.offsetTop+cur.offsetHeight+"px";
el.style.display = "block";
document.onclick = function(e) {
var obj = document.all ? event.srcElement : e.target;
if (obj != el && obj != cur) {
el.style.display = "none";
}
}
}
</script>

</head>

<body>

<a href="#" onClick="show(this,'dlg1'); return false;">Test</a>

<div id="dlg1" class="dialog">
Text to show.
</div>

</body>
</html>
mrjokers is offline  
Add Post to del.icio.us
Reply With Quote
Sponsored Links
SEO Hosting by HostGator  Advertise Here  Buy Blog Links
Old 12-01-2007, 03:25 PM   #2 (permalink)
v7n Mentor
 
Costin Trifan's Avatar
 
Join Date: 04-13-07
Location: Romania
Posts: 2,939
iTrader: 0 / 0%
Costin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web proCostin Trifan is a highly respected web pro
Firstly, welcome to v7n, mrjokers!

Now, to answer your question:
1. you need to delete one line from your code (the bolded comented one):
Code:
var el = document.getElementById(element); el.style.left = cur.offsetLeft+"px"; el.style.top = cur.offsetTop+cur.offsetHeight+"px"; //el.style.display = "block";
2. add this lines of code:
Code:
if ( el.style.display == 'none' ) { el.style.display = 'block'; } else { el.style.display = 'none'; }
3. finally, your code should look like this:
Code:
function show(cur,element) { var el = document.getElementById(element); el.style.left = cur.offsetLeft+"px"; el.style.top = cur.offsetTop+cur.offsetHeight+"px"; document.onclick = function(e) { var obj = document.all ? event.srcElement : e.target; if (obj != el && obj != cur) { el.style.display = "none"; } } if ( el.style.display == 'none' ) { el.style.display = 'block'; } else { el.style.display = 'none'; } }
__________________
JUNE - JavaScript Framework

Last edited by Costin Trifan : 12-01-2007 at 03:30 PM.
Costin Trifan is offline  
Add Post to del.icio.us
Reply With Quote
Old 12-01-2007, 03:41 PM   #3 (permalink)
v7n Mentor
 
Join Date: 07-24-06
Posts: 642
iTrader: 1 / 100%
Latest Blog:
None

nasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nicenasty.web is just really nice
Little update to this

Change
Code:
if ( el.style.display == 'none' )
into
Code:
if ( el.style.display != 'block' )
(because the first code has a bug when you have to click on a link two times to get it working)
nasty.web is online now  
Add Post to del.icio.us
Reply With Quote
Go Back   Webmaster Forum > Web Development > Web Design Lobby > Coding Forum

Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
javascript help mtaylor314 Coding Forum 2 05-04-2007 03:46 PM
Count-up from a particular year + Javascript within Javascript? Help! Azam.biz Coding Forum 3 06-06-2005 06:47 AM
Javascript Help. samer Coding Forum 2 09-08-2004 07:50 AM
I need some help with JavaScript. Can anyone help me please? CrAg Coding Forum 2 03-22-2004 05:56 AM
javascript webmaster@lostinteardrops Coding Forum 16 10-31-2003 08:19 AM


Sponsor Links
Get exposure! Get exposure! Find Scripts Web Hosting Directory Get exposure! SEO Blog


All times are GMT -7. The time now is 12:50 PM.
© Copyright 2008 V7 Inc