Solved

placing a div exactly div below a td

Posted on 2010-09-01
5
483 Views
Last Modified: 2013-11-19
I have an image inside a td. On mouse hovering the image, another div should show exactly below the td. It should not push the contents of the page down, instead just open it on op of the exisitng page contents. Please help me how I can position the div so that it is displayed excatly below the td. Thanks

I have attcahed the html and javascript that I have.
function HideCart(id) 
        {
            if (id.length < 1) 
            {
                return; 
            }
            document.getElementById(id).style.display = "none";
        }

        function ShowCart(id) 
        {
            
            if (id.length < 1) {
                return; 
            }
            document.getElementById(id).style.display = "block";
        }

<img src="Assets/Images/Master/shoppingcart.jpg" 
                                     style="height: 34px; width: 60px; " alt=' '  onmouseover="ShowCart('divCart')" onmouseout="HideCart('divCart');" />
                                <div    id="divCart"    style="display:none;       position:absolute;       border-style: solid; background-color: #E5E5E5; height: 4px;      padding: 5px;" class="search">
 ctent here</div>

Open in new window

0
Comment
Question by:TrialUser
5 Comments
 
LVL 14

Accepted Solution

by:
Daniel Junges earned 250 total points
ID: 33576314

<div    id="divCart"    style="display:none; position:relative; .....

function ShowCart(id) {
      var ele = document.getElementById(id);
      var ele2 = document.getElementById("tdShoppingCart");
      ele.style.Top = ele2.style.top + ele2.style.height;
      ele.style.display = "block";                        
}
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 125 total points
ID: 33576319
You can try to retrieve the top and left positions of the TD on hover, and positioning your div dinamically on same left and a top equal to the td's + it's height.

I'm not sure if this will work well, as every browser has it's own conception of positioning.

I'd rather prefer to seek a solution base on design. Maybe you should allow enough space in your td below the image to fit there your div, and show or hide it on hover event.

0
 

Author Comment

by:TrialUser
ID: 33577023
junges:
I get the ele2 as null. I even tried to pass the element id to the javascript but still it shows null.

Please help
"ShowCart('tdShoppingCart','divCart');"

  function ShowCart(idTD,id)
        {

            if (id.length < 1)
            {
                return;
            }
            var objTD = document.getElementById(idTD);
            alert(objTD);
            var objCart = document.getElementById(id);
            alert(objCart);
            objCart.Style.Display = "block";
            objCart.style.top = objTD.style.top + objTD.style.height;
 
                    }
0
 
LVL 14

Assisted Solution

by:Daniel Junges
Daniel Junges earned 250 total points
ID: 33579025
hi TrialUser,

its null because you <td> has the argument runat="server", can you remove it ?
0
 
LVL 22

Assisted Solution

by:kadaba
kadaba earned 125 total points
ID: 33579276
you might try it this way....
using jquery would make the code nice

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Sample</title>
<style type="text/css">
.hiddenDiv
{
	position:absolute;
	border:solid 1px #aaaaaa;
	background-color:#FFEE00;
	width:150px;
	height:150px;
}
.oddTr td{
	background-color:#E1E1E1;
}
.evenTr td{
	background-color:#FFFFFF;
}
</style>
 
<script type="text/javascript">

function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
 
function ShowCart(parent,cartId,content)
{
	var divCart =  $(cartId);
	divCart.innerHTML = content;
	
	var pos = findPos(parent);
	divCart.style.left = (pos[0] + 20) + "px";
	divCart.style.top = (pos[1] + 20) + "px";
	divCart.style.display="block";
}
 
function $(id){
	return document.getElementById(id);
} 

function HideCart(cartId)
{
	var divCart = $(cartId);
	divCart.style.display = "none";
}
</script>
</head>
<body>
<div class="hiddenDiv" id="divCart" style="display:none;">
</div>
<table border="1" width="300px">
	<tr class="oddTr">
		<td>
			No action
		</td>
		<td style="cursor:pointer;" onmouseover="ShowCart(this,'divCart','Lorem ipsum dolor sit amet');" onmouseout="HideCart('divCart')">
			Mouse over me
		</td>
	</tr>
	<tr class="evenTr">
		<td style="cursor:pointer;" onmouseover="ShowCart(this,'divCart','consectetur adipisicing elit, sed do eiusmod ');" onmouseout="HideCart('divCart')">
			Mouse over me
		</td>
		<td>
			No action
		</td>
	</tr>
	<tr class="oddTr">
		<td>
			No action
		</td>
		<td style="cursor:pointer;" onmouseover="ShowCart(this,'divCart','Lorem ipsum dolor sit amet');" onmouseout="HideCart('divCart')">
			Mouse over me
		</td>
	</tr>
	<tr class="evenTr"> 
		<td style="cursor:pointer;" onmouseover="ShowCart(this,'divCart','consectetur adipisicing elit, sed do eiusmod ');" onmouseout="HideCart('divCart')">
			Mouse over me
		</td>
		<td>
			No action
		</td>
	</tr>
</table> 
</body>
</html>

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question