Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-07-2011, 05:26 PM   PM User | #1
Rain Lover
Regular Coder

 
Join Date: Nov 2009
Posts: 138
Thanks: 9
Thanked 0 Times in 0 Posts
Rain Lover is an unknown quantity at this point
Put inline JavaScript in the head

Hi,

The following code works:

Code:
<html>
<head>
<style type="text/css">
img
{
opacity:0.4;
filter:alpha(opacity=40);
}
</style>
</head>
<body>

<img src="http://www.w3schools.com/Css/klematis.jpg" width="150" height="113" alt="klematis"
onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />

<img src="http://www.w3schools.com/Css/klematis2.jpg" width="150" height="113" alt="klematis"
onmouseover="this.style.opacity=1;this.filters.alpha.opacity=100"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />

</body>
</html>
But the problem is you have to repeat the inline JavaScript for all <img> tags. I tried to put the script in the head to no avail:

Code:
<html>
<head>
<style type="text/css">
img
{
opacity:0.4;
filter:alpha(opacity=40);
}
</style>
<script type="text/javascript">
function getElements()
  {
  var x=document.getElementsByTagName("img");
  x.style.opacity=1; x.filters.alpha.opacity=100;
  }
</script>
</head>
<body>

<img src="http://www.w3schools.com/Css/klematis.jpg" width="150" height="113" alt="klematis"
onmouseover="getElements()"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />

<img src="http://www.w3schools.com/Css/klematis2.jpg" width="150" height="113" alt="klematis"
onmouseover="getElements()"
onmouseout="this.style.opacity=0.4;this.filters.alpha.opacity=40" />

</body>
</html>
Everything seems right to me, but it doesn't work.

Many thanks for any help!
Mike
Rain Lover is offline   Reply With Quote
Old 07-07-2011, 06:35 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
<html>
<head>
<style type="text/css">
img
{
opacity:0.4;
filter:alpha(opacity=40);
}
</style>
<script type="text/javascript">
function opaque(e)
  {
 e.style.opacity=1;
if(e.filters)e.filters.alpha.opacity=100
  }
function transparent(e)
  {
 e.style.opacity = .4;
 if(e.filters)e.filters.alpha.opacity = 40; 
  }


</script>


</head>
<body>

<img src="klematis.jpg" 
onmouseover="opaque(this)"
onmouseout="transparent(this)" />

<img src="klematis2.jpg" 
onmouseover="opaque(this)"
onmouseout="transparent(this)" />



</body>
</html>
DaveyErwin is offline   Reply With Quote
Old 07-07-2011, 06:51 PM   PM User | #3
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 806
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
This might be more like what
you have in mind ....

Code:
<html>
<head>
<style type="text/css">
img
{
opacity:0.4;
filter:alpha(opacity=40);
}
</style>
<script type="text/javascript">
function opaque(){
   this.style.opacity=1;
   if(this.filters)this.filters.alpha.opacity=100
  }
function transparent(){
   this.style.opacity = .4;
   if(this.filters)this.filters.alpha.opacity = 40; 
  }

function init(){
 var x = document.getElementsByTagName("IMG");
 for(var i = x.length;i--;){
  x[i].onmouseover=opaque;
  x[i].onmouseout=transparent;
 }
}
</script>


</head>
<body onload="init()">

<img src="klematis.jpg"  />

<img src="klematis2.jpg" />



</body>
</html>
DaveyErwin is offline   Reply With Quote
Old 07-07-2011, 09:15 PM   PM User | #4
Rain Lover
Regular Coder

 
Join Date: Nov 2009
Posts: 138
Thanks: 9
Thanked 0 Times in 0 Posts
Rain Lover is an unknown quantity at this point
Thank you!
Rain Lover is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:36 PM.


Advertisement
Log in to turn off these ads.