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 04-22-2008, 07:13 PM   PM User | #1
Remotive
New Coder

 
Join Date: Jan 2008
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Remotive is an unknown quantity at this point
Function has no properties.

Heya people, well I have this function...

Code:
 	<script type="text/javascript">
var divs = ['firstdiv', 'seconddiv', 'thirddiv', 'fourthdiv', 'fifthdiv']; 
    function changeBGColor(a){ 
        for(i=0; i<divs.length; i++){ 
        document.getElementById(divs[i]).style.backgroundImage = 'url(../images/catalogue/slot_mask.png)'; 
        } 
    document.getElementById(a).style.backgroundImage = 'url(../images/catalogue/selector.png)'; 
    }
</script>
...and when I look at the error in FireBug, it says that document.getElementById(divs[i]) has no properties and I was wondering, what is wrong with it?

Please help people...
Remotive is offline   Reply With Quote
Old 04-22-2008, 07:59 PM   PM User | #2
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Run the script onload, or place it in the body of the page,after you define the elements the script needs.
mrhoo is offline   Reply With Quote
Old 04-22-2008, 07:59 PM   PM User | #3
coothead
Senior Coder

 
coothead's Avatar
 
Join Date: Jan 2004
Location: chertsey, a small town 25 miles south west of london, england.
Posts: 1,545
Thanks: 0
Thanked 195 Times in 191 Posts
coothead will become famous soon enough
Hi there Remotive,

and a warm welcome to these forums.

There is nothing wrong, with the snippet of code that you have posted.

As I did not have the benefit of your images or the rest of the page markup, I had to test it with my own...
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<base href="http://mysite.orange.co.uk/azygous/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container div {
    width:100px;
    height:100px;
    background-image:url(images/anim.gif);
    border:1px solid #000;
    margin:2px;
    cursor:pointer;
 }
</style>

<script type="text/javascript">
window.onload=function(){
   var mydiv=document.getElementById('container').getElementsByTagName('div');
for(var c=0;c<mydiv.length;c++) {
mydiv[c].onclick=function() {
   changeBGColor(this.id);
   }
  }
 }

var divs = ['firstdiv', 'seconddiv', 'thirddiv', 'fourthdiv', 'fifthdiv']; 

function changeBGColor(a){ 
for(i=0; i<divs.length; i++){ 
   document.getElementById(divs[i]).style.backgroundImage = 'url(images/anim.gif)'; 
 } 
   document.getElementById(a).style.backgroundImage = 'url(images/anim2.gif)'; 
 }
</script>

</head>
<body>

<div id="container">
 <div id="firstdiv"></div>
 <div id="seconddiv"></div>
 <div id="thirddiv"></div>
 <div id="fourthdiv"></div>
 <div id="fifthdiv"></div>
</div>

</body>
</html>
You will see that the only change to your script, that I have made, is the insertion of my images.

coothead
coothead is offline   Reply With Quote
Old 04-22-2008, 08:04 PM   PM User | #4
Remotive
New Coder

 
Join Date: Jan 2008
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Remotive is an unknown quantity at this point
Quote:
Originally Posted by coothead View Post
Hi there Remotive,

and a warm welcome to these forums.

There is nothing wrong, with the snippet of code that you have posted.

As I did not have the benefit of your images or the rest of the page markup, I had to test it with my own...
Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<base href="http://mysite.orange.co.uk/azygous/">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
#container div {
    width:100px;
    height:100px;
    background-image:url(images/anim.gif);
    border:1px solid #000;
    margin:2px;
    cursor:pointer;
 }
</style>

<script type="text/javascript">
window.onload=function(){
   var mydiv=document.getElementById('container').getElementsByTagName('div');
for(var c=0;c<mydiv.length;c++) {
mydiv[c].onclick=function() {
   changeBGColor(this.id);
   }
  }
 }

var divs = ['firstdiv', 'seconddiv', 'thirddiv', 'fourthdiv', 'fifthdiv']; 

function changeBGColor(a){ 
for(i=0; i<divs.length; i++){ 
   document.getElementById(divs[i]).style.backgroundImage = 'url(images/anim.gif)'; 
 } 
   document.getElementById(a).style.backgroundImage = 'url(images/anim2.gif)'; 
 }
</script>

</head>
<body>

<div id="container">
 <div id="firstdiv"></div>
 <div id="seconddiv"></div>
 <div id="thirddiv"></div>
 <div id="fourthdiv"></div>
 <div id="fifthdiv"></div>
</div>

</body>
</html>
You will see that the only change to your script, that I have made, is the insertion of my images.

coothead
Thank you so much for the warm welcoming, but sad to say your code did not work for me, I don't know why either I just keep getting the same error.
Remotive is offline   Reply With Quote
Old 04-22-2008, 08:14 PM   PM User | #5
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
hrm.... this is most commonly caused by different casing between the ids and the ids used for the lookup... for example

Code:
 
document.getElementById("happy").style
will give you the same message on

Code:
 
<div id="Happy"></div>
<div id="HAPPY"></div>
<div id="hAppy"></div>
etc., the only one it will actually work on is

Code:
 
<div id="happy"></div>
so be absolutely certain about the cases used. also, if the location you're attempting to change the backgrounds from is in a different window (ie, a popup, or different frame maybe) the syntax to access the containing element is different as well.

if you could please post the full code you're having issues with, if in fact they remain issues hereafter, it would be most helpful.

i too would like to welcome you to the forum, we hope you enjoy your stay. the continental breakfast is from 7am-9am your time, checkin is at 3pm and check out is at 11am. if you need anything else, the front desk is open until 10:30pm
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Old 04-22-2008, 08:23 PM   PM User | #6
Remotive
New Coder

 
Join Date: Jan 2008
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Remotive is an unknown quantity at this point
Code:
<style type="text/css">
<!--
div {
position: relative;
width: 61px;
height: 61px;
background-image: url(../images/catalogue/slot_mask.png);
}
-->
</style> 
<script type="text/javascript">
var divs = new Array("firstdiv", "seconddiv", "thirddiv", "fourthdiv", "fifthdiv"); 
    function changeBGColor(a){ 
        for(i=0;i<divs.length;i++){ 
        document.getElementById(divs[i]).style.backgroundImage = 'url(../images/catalogue/slot_mask.png)'; 
        } 
    document.getElementById(a).style.backgroundImage = 'url(../images/catalogue/selector.png)'; 
    }
</script>
<div id="firstdiv" onclick="changeBGColor('firstdiv');">content</div>
<div id="seconddiv" onclick="changeBGColor('seconddiv');">content</div>
<div id="thirddiv" onclick="changeBGColor('thirddiv');">content</div>
<div id="fourthdiv" onclick="changeBGColor('fourthdiv');">content</div>
That is the I am using (full oage by the way).

Hope this helps you sort out any problems.
Remotive is offline   Reply With Quote
Old 04-22-2008, 08:25 PM   PM User | #7
coothead
Senior Coder

 
coothead's Avatar
 
Join Date: Jan 2004
Location: chertsey, a small town 25 miles south west of london, england.
Posts: 1,545
Thanks: 0
Thanked 195 Times in 191 Posts
coothead will become famous soon enough
Hi there Remotive,
Quote:
...but sad to say your code did not work for me
I am really sorry to hear this.

I tested it in IE6, IE7, Opera 9, Safari 3 and Firefox 2 without any problems.

Is it possible that you are the proud possessor of some obscure obstinate browser.

coothead
coothead is offline   Reply With Quote
Old 04-22-2008, 08:28 PM   PM User | #8
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
[ICODE]var divs = new Array("firstdiv", "seconddiv", "thirddiv", "fourthdiv", "fifthdiv");/ICODE]
change to
var divs = ["firstdiv", "seconddiv", "thirddiv", "fourthdiv", "fifthdiv"];

if you need a further debugging tool put
alert(divs[i]);
just inside the for loop, it will inform you of the name it will be performing the lookup by.

and just out of curiosity, if you don't actually have the document structured..

Code:
 
<html>
<head>
<!-- your style and script here -->
</head>
<body>
<!-- your divs -->
</body>
</head>
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet is offline   Reply With Quote
Old 04-23-2008, 03:14 PM   PM User | #9
Remotive
New Coder

 
Join Date: Jan 2008
Posts: 30
Thanks: 1
Thanked 0 Times in 0 Posts
Remotive is an unknown quantity at this point
Yeah, I have now structed the page and changes the code but still nothing...
Remotive is offline   Reply With Quote
Old 04-23-2008, 03:28 PM   PM User | #10
mjlorbet
Regular Coder

 
mjlorbet's Avatar
 
Join Date: Jan 2008
Location: Milwaukee, WI
Posts: 724
Thanks: 8
Thanked 96 Times in 95 Posts
mjlorbet will become famous soon enough
clear the cache on the browser you're testing
__________________
-Mike
"Want me to precludify him, like some kind of dispatcherator?... Can do!" -Bender
mjlorbet 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 12:47 PM.


Advertisement
Log in to turn off these ads.