[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

placing a div exactly div below a td

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
TrialUser
Asked:
TrialUser
4 Solutions
 
Daniel JungesCommented:

<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
 
BardobraveCommented:
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
 
TrialUserAuthor Commented:
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
 
Daniel JungesCommented:
hi TrialUser,

its null because you <td> has the argument runat="server", can you remove it ?
0
 
kadabaCommented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now