www.webdeveloper.com
Results 1 to 5 of 5

Thread: Get ID of element on mouseover

  1. #1
    Join Date
    Oct 2008
    Posts
    28

    Get ID of element on mouseover

    I have a script which needs to get the ID of an element on mouseover and then write this id in a popup div.

    I've got no idea how to go about getting the id of the element.

    Can anyone help pelase.

    Code:
    <script type="text/javascript">
    
    	$title = "Default Title";
    	
    	function getContent()
    	{		
    			// Get id if element which is hovered over and show title - How do you  do this 
    			
    			
    			$content = $title;	
    			return $content;
    	}
    
    	function addContent() 
    	{
    			getContent(); // call getContent() function to get the content to be added to the popup
    			
    			var e = document.getElementById("popup"); 
    			if(e)  e.innerHTML = $content; // Add content to popup div
    	}
    	
    	function removeContent() 
    	{
    			var e = document.getElementById("popup");
    			if(e)  e.innerHTML = ""; // Remove content from popup div
    	}
    	
    	</script>
    
    <div id="Title One" class="leaf1"  onmouseover="addContent();" onmouseout="removeContent()"></div><br />
    <div id="Title Two" class="leaf2"  onmouseover="addContent();" onmouseout="removeContent()"></div><br />
    <div id="Title Three" class="leaf3"  onmouseover="addContent();" onmouseout="removeContent()"></div><br />
    
    <div id="popup"></div>
    Thanks

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Pass the id:
    Code:
    <div id="Title One" class="leaf1"  onmouseover="addContent(this.id);"
    Your id's are invalid: http://www.w3.org/TR/html4/types.html#type-id

  3. #3
    Join Date
    Oct 2008
    Posts
    28
    thanks for your help.

    I've managed to get the script working now, well in IE.

    It now has a function to position the popup near the mouse pointer. However it only works in IE, and not Firefox.

    Can anyone enlighten me as to why.

    Thanks.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    
    <style type="text/css">
    
    .leaf1 {
    width:100px;
    height:100px;
    border:1px solid #ccc;
    background:red;
    }
    .leaf2 {
    width:100px;
    height:100px;
    border:1px solid #ccc;
    background:orange;
    }
    .leaf3 {
    width:100px;
    height:100px;
    border:1px solid #ccc;
    background:green;
    }
    
    #popup {
    width:300px;
    background:yellow;
    border:1px solid #ccc;
    display:none;
    position:absolute;
    }
    
    
    </style>
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    
    	$title = "Default Title";
    
    	var IE = document.all?true:false;
    	if (!IE) 
    	
    		document.captureEvents(Event.MOUSEMOVE)
    		document.onmousemove = getMouseXY;
    		var tempX = 0;
    		var tempY = 0;
    		
    		function getMouseXY(e) 
    		{
    				if (IE) {
    						tempX = event.clientX;
    						tempY = event.clientY;
    				
    				} else {
    						tempX = e.pageX;
    						tempY = e.pageY;
    				}
    				
    				if (tempX < 0){tempX = 0;}
    				if (tempY < 0){tempY = 0;}
    	
    				return true;
    		}
    		
    
    
    	function getContent($title)
    	{		
    			$content = $title;	
    			return $content;
    	}
    
    	function addContent($title) 
    	{
    			getContent($title); // call getContent() function to get the content to be added to the popup
    			
    			var e = document.getElementById('popup'); 
    			if(e)  e.innerHTML = $content; // Add content to popup div
    			
    			showPopUp();
    	}
    	
    	function removeContent() 
    	{
    			var e = document.getElementById("popup");
    			if(e)  e.innerHTML = ""; // Remove content from popup div
    			
    			hidePopUp();
    	}
    	
    	function showPopUp() 
    	{
    			document.getElementById('popup').style.display = 'block';
    			document.getElementById('popup').style.top=tempY;
    			document.getElementById('popup').style.left=tempX;
    
    	}
    
    	function hidePopUp() 
    	{
    			document.getElementById("popup").style.display = 'none';
    
    	}
    	
    	</script>
    
    <div title="Title One" class="leaf1"  onmouseover="addContent(this.title);" onmouseout="removeContent()"></div><br />
    <div title="Title Two" class="leaf2"  onmouseover="addContent(this.title);" onmouseout="removeContent()"></div><br />
    <div title="Title Three" class="leaf3"  onmouseover="addContent(this.title);" onmouseout="removeContent()"></div><br />
    
    <div id="popup"></div>
    
    </body>
    </html>

  4. #4
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    	function showPopUp() 
    	{
    			document.getElementById('popup').style.display = 'block';
    			document.getElementById('popup').style.top=tempY+'px';
    			document.getElementById('popup').style.left=tempX+'px';
    
    	}

  5. #5
    Join Date
    Oct 2008
    Posts
    28
    thanks very much.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles