/    Sign up×
Community /Pin to ProfileBookmark

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>
[/code]

Thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangOct 23.2008 — Pass the id:&lt;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
Copy linkTweet thisAlerts:
@keeps21authorOct 23.2008 — 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.

<i>
</i>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;

&lt;style type="text/css"&gt;

.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;
}


&lt;/style&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;script type="text/javascript"&gt;

<i> </i>$title = "Default Title";

<i> </i>var IE = document.all?true:false;
<i> </i>if (!IE)

<i> </i> document.captureEvents(Event.MOUSEMOVE)
<i> </i> document.onmousemove = getMouseXY;
<i> </i> var tempX = 0;
<i> </i> var tempY = 0;

<i> </i> function getMouseXY(e)
<i> </i> {
<i> </i> if (IE) {
<i> </i> tempX = event.clientX;
<i> </i> tempY = event.clientY;

<i> </i> } else {
<i> </i> tempX = e.pageX;
<i> </i> tempY = e.pageY;
<i> </i> }

<i> </i> if (tempX &lt; 0){tempX = 0;}
<i> </i> if (tempY &lt; 0){tempY = 0;}

<i> </i> return true;
<i> </i> }



<i> </i>function getContent($title)
<i> </i>{
<i> </i> $content = $title;
<i> </i> return $content;
<i> </i>}

<i> </i>function addContent($title)
<i> </i>{
<i> </i> getContent($title); // call getContent() function to get the content to be added to the popup

<i> </i> var e = document.getElementById('popup');
<i> </i> if(e) e.innerHTML = $content; // Add content to popup div

<i> </i> showPopUp();
<i> </i>}

<i> </i>function removeContent()
<i> </i>{
<i> </i> var e = document.getElementById("popup");
<i> </i> if(e) e.innerHTML = ""; // Remove content from popup div

<i> </i> hidePopUp();
<i> </i>}

<i> </i>function showPopUp()
<i> </i>{
<i> </i> document.getElementById('popup').style.display = 'block';
<i> </i> document.getElementById('popup').style.top=tempY;
<i> </i> document.getElementById('popup').style.left=tempX;

<i> </i>}

<i> </i>function hidePopUp()
<i> </i>{
<i> </i> document.getElementById("popup").style.display = 'none';

<i> </i>}

<i> </i>&lt;/script&gt;

&lt;div title="Title One" class="leaf1" onmouseover="addContent(this.title);" onmouseout="removeContent()"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div title="Title Two" class="leaf2" onmouseover="addContent(this.title);" onmouseout="removeContent()"&gt;&lt;/div&gt;&lt;br /&gt;
&lt;div title="Title Three" class="leaf3" onmouseover="addContent(this.title);" onmouseout="removeContent()"&gt;&lt;/div&gt;&lt;br /&gt;

&lt;div id="popup"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;

Copy linkTweet thisAlerts:
@FangOct 23.2008 —  function showPopUp()
{
document.getElementById('popup').style.display = 'block';
document.getElementById('popup').style.top=tempY[COLOR="Blue"]+'px'[/COLOR];
document.getElementById('popup').style.left=tempX[COLOR="Blue"]+'px'[/COLOR];

<i> </i>}
Copy linkTweet thisAlerts:
@keeps21authorOct 23.2008 — thanks very much.?
×

Success!

Help @keeps21 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...