/    Sign up×
Community /Pin to ProfileBookmark

JS variable.onMouseOver = not working, why?

Hello again!

On the below code:

[code]
<script type=”text/javascript”>

// DeltaOne
// thanks to Mr J from codingforums.com

var offsetx = 100
var offsety = -150
var elementID=”myImg”

function showImg(obj,image){

if(!document.getElementById(elementID)){
newImg = document.createElement(‘IMG’)
newImg.id = elementID
newImg.style.display=”none”
newImg.style.position=”absolute”
document.body.appendChild(newImg)
}

imageDisplay = document.getElementById(elementID)
imageDisplay.src = image
imageDisplay.style.display = ‘block’

imageDisplay.style.left=obj.offsetLeft+offsetx
imageDisplay.style.top=obj.offsetTop+offsety

if(imageDisplay.offsetLeft<0){
imageDisplay.style.left=0
}

if(imageDisplay.offsetLeft+imageDisplay.offsetWidth>document.body.clientWidth){
imageDisplay.style.left=document.body.clientWidth – imageDisplay.offsetWidth-50
}

}

function hideImg(){
imageDisplay.style.display=”none”
}

</script>

<style>
#myImg {
width:150px;
border:1px solid #5555aa;
}

</style>
[/code]

If I put the below:

[code]<a onmouseover=”showImg(this,’http://picture.jpg’)” onmouseout=”hideImg()”>link</a>[/code]

On mouseOver over the ‘link’ a new picture will appear (offsetted by some co-ordinates, like the script tells us) – and it works perfectly.

Now, I have a problem..

If I put the below code instead of the one above that works perfectly:

[code]
n= document.createElement(‘a’)
n.href = “http://127.0.0.1″
n.onmouseover=”showImg(this,’picture.jpg’)”
n.onmouseout=”hideImg()”
document.body.appendChild(n)

document.write(‘<br><br>’ + n + ‘<br><br>’)
[/code]

The page shows the actual .href (you can not click on it though), but the onMouseOver and onMouseOut attributes are lost, and no new picture will appear (offsetted by X-Y coordonates)

It’s somewhat confusing to describe this – but I figure that I made things at least a little clear. Does anybody have any idea how to fix this, and to make it work?

[quote]

I want to make that element so I can modify the below code
t[t.length] = [“a_name”,”<a onmouseover=’showImg(this,’http://picture.jpg‘)’ onmouseout=’hideImg()’><img src=’http://thingy.gif‘ alt=”></a>”]

to
t[t.length] = [“a_name”,n]

But neither having the above code in that huge t[t.lenth] form won’t make it work,
1-st it gives a syntax error – i figure its because of the onmouseout attribute that has to declare itself using single quotes and to declare the img src with single quotes, so using in two instances single quotes,
2nd – it can not find the showImg function if I onMouseOver the pic

[/quote]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CrazyMerlinFeb 28.2007 — <i>
</i>var b = document.createElement('br');
var n = document.createElement('a');
var i = document.createElement('img');
i.src = "http://thingy.gif";
i.alt = "";
n.appendChild(i);
n.href = "http://127.0.0.1";
n.setAttribute("onmouseover", "showImg(this,'picture.jpg')");
n.setAttribute("onmouseout", "hideImg()");

t[t.length] = n;


you can test it with:
<i>
</i>document.body.appendChild(b);
document.body.appendChild(b);
document.body.appendChild(n);
document.body.appendChild(b);
document.body.appendChild(b);


untested and a little messy but it should do.

just remember that what is in t is an object, not a bunch of html.

to show the html, use: t[number].outerHTML;
Copy linkTweet thisAlerts:
@DeltaOneauthorFeb 28.2007 — Oh, thanks for the help!

I'm not at home right now, but will certaily test it once I get there.

Hopefully it'll work..
Copy linkTweet thisAlerts:
@DeltaOneauthorFeb 28.2007 — Unfortunately, it still doesn't work, which is pretty odd...

The new script gives no error whatsoever, the body appends the 'n' variable (which is an image, and the a href goes where it should), but somehow the onMouseOver and onMouseOut things get lost..

For example, my html code is this:
<i>
</i>&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;Document Title&lt;/TITLE&gt;

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

var offsetx = 100
var offsety = 20
var elementID="myImg"

function showImg(obj,image){

if(document.createElement){

if(!document.getElementById(elementID)){
newImg = document.createElement('IMG')
newImg.id = elementID
newImg.style.display="none"
newImg.style.position="absolute"
document.body.appendChild(newImg)
}

imageDisplay = document.getElementById(elementID)
imageDisplay.src = image
imageDisplay.style.display = 'block'

imageDisplay.style.left=obj.offsetLeft+offsetx
imageDisplay.style.top=obj.offsetTop+offsety

if(imageDisplay.offsetLeft&lt;0){
imageDisplay.style.left=0
}

if(imageDisplay.offsetLeft+imageDisplay.offsetWidth&gt;document.body.clientWidth){
imageDisplay.style.left=document.body.clientWidth - imageDisplay.offsetWidth-50
}

}
}

function hideImg(){
imageDisplay.style.display="none"
}

&lt;/script&gt;

&lt;style&gt;
#myImg {
width:150px;
border:1px solid #5555aa;
}

&lt;/style&gt;

&lt;/HEAD&gt;
&lt;BODY&gt;

&lt;a onmouseover="showImg(this,'http://i3.photobucket.com/albums/y66/FirstPhotoLast/bah.jpg')" onmouseout="hideImg()"&gt;link&lt;/a&gt;&lt;br&gt;&lt;br&gt;

&lt;a onmouseover="showImg(this,'http://i3.photobucket.com/albums/y66/FirstPhotoLast/bah2.jpg')" onmouseout="hideImg()"&gt;link&lt;/a&gt;
&lt;br&gt;
&lt;br&gt;
&lt;script&gt;
var b = document.createElement('br');
var n = document.createElement('a');
var i = document.createElement('img');
i.src = "http://i3.photobucket.com/albums/y66/FirstPhotoLast/bah.jpg";
i.alt = "";
n.appendChild(i);
n.href = "#";
n.setAttribute("onmouseover", "showImg(this,'http://i3.photobucket.com/albums/y66/FirstPhotoLast/bah2.jpg')");
n.setAttribute("onmouseout", "hideImg()");

t[t.length] = n;

&lt;/script&gt;
&lt;script&gt;
document.body.appendChild(b);
document.body.appendChild(b);
document.body.appendChild(n);
document.body.appendChild(b);
document.body.appendChild(b);

&lt;/script&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
Copy linkTweet thisAlerts:
@DeltaOneauthorMar 06.2007 — I read somewhere that if you create a new element, give attributes to it and then append it to the document, the attributes won't be initiated. (which is pretty much my problem here.)


I also read there that if you want to have those attributes to the element that you appended to the document, all you had to do is to re-initialize it, or something, as I unfortunately forgot the page talking about it, and I briefly remember the discussion.

Anyone know how I can re-initialize that n variable?

something like n = new n, if it exist..
×

Success!

Help @DeltaOne 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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