onMouseOver & arrays - using style to modify border

B

bundaism

The code below is meant to go through all images on the page, and add a
red border on the mouseover, and a white border on the mouseout. It's
not working and I've spend too many hours on this - please help!

Thanks... code below....

<img src="someImage.jpg">

<script type="text/javascript">
var img_elems = document.getElementsByTagName("img");

for (var i=0;i<img_elems.length;++i)
{
var img_src = img_elems.src;
img_elems.onmouseover="this.style.borderColor='red'";
img_elems.onmouseout="this.style.borderColor='white'";

}
</script>
 
W

web.dev

The code below is meant to go through all images on the page, and add a
red border on the mouseover, and a white border on the mouseout. It's
not working and I've spend too many hours on this - please help!

Thanks... code below....

<img src="someImage.jpg">

<script type="text/javascript">
var img_elems = document.getElementsByTagName("img");

for (var i=0;i<img_elems.length;++i)
{
var img_src = img_elems.src;
img_elems.onmouseover="this.style.borderColor='red'";
img_elems.onmouseout="this.style.borderColor='white'";

}
</script>


Try the following:

<script type = "text/javascript">
function setup()
{
var len = document.images.length;

for(var i = 0; i < len; ++i)
{
assignEvent(i);
}

}

function assignEvent(index)
{
document.images[index].onmouseout = mOut;
document.images[index].onmouseover = mOver;
}

function mOut()
{
this.style.border = "1px solid white";
}

function mOver()
{
this.style.border = "1px solid red";
}

window.onload = setup;
</script>

For border color to work, you'll need border width. That's the reason
for using border instead of assigning all of those separate css values.
 
B

bundaism

THanks it works!
web.dev said:
The code below is meant to go through all images on the page, and add a
red border on the mouseover, and a white border on the mouseout. It's
not working and I've spend too many hours on this - please help!

Thanks... code below....

<img src="someImage.jpg">

<script type="text/javascript">
var img_elems = document.getElementsByTagName("img");

for (var i=0;i<img_elems.length;++i)
{
var img_src = img_elems.src;
img_elems.onmouseover="this.style.borderColor='red'";
img_elems.onmouseout="this.style.borderColor='white'";

}
</script>


Try the following:

<script type = "text/javascript">
function setup()
{
var len = document.images.length;

for(var i = 0; i < len; ++i)
{
assignEvent(i);
}

}

function assignEvent(index)
{
document.images[index].onmouseout = mOut;
document.images[index].onmouseover = mOver;
}

function mOut()
{
this.style.border = "1px solid white";
}

function mOver()
{
this.style.border = "1px solid red";
}

window.onload = setup;
</script>

For border color to work, you'll need border width. That's the reason
for using border instead of assigning all of those separate css values.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top