Solved

placing a div exactly div below a td

Posted on 2010-09-01
5
485 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Find out what you should include to make the best professional email signature for your organization.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

838 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