Solved

placing a div exactly div below a td

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Ajax control editor 4 35
Accessing property value in an object. 1 18
Need sample Angular apps for study 4 35
Problem to page 4 26
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now