/    Sign up×
Community /Pin to ProfileBookmark

image.src doesn’t change

hi all,

I’ve got this piece of malfunctioning code:

in <head></head> :

[CODE]
<script type=”text/javascript”>
function defaultPieces(){
clr = document.chess.side.value;
if(clr == ‘b’){
oclr = ‘w’;
}
else{
oclr = ‘b’;
}
for(i=1;i<9;i++){
document.getElementById(i+’B’).src = “images/pion_”+oclr+”.gif”;
document.getElementById(i+’G’).src = “images/pion_”+clr+”.gif”;
}
document.getElementById(‘1A’).src = “images/tower_”+oclr+”.gif”;
document.getElementById(‘8A’).src = “images/tower_”+oclr+”.gif”;
document.getElementById(‘1H’).src = “images/tower_”+clr+”.gif”;
document.getElementById(‘8H’).src = “images/tower_”+clr+”.gif”;
}
</script>
[/CODE]

and this in my <body onLoad=”javascript:init();”></body> tags:

[code=html]
<tr>
<td style=”width:75px;height:75px;background-color:black”><img src=”images/blank.gif” name=”1B” id=”2B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;”><img src=”images/blank.gif” name=”2B” id=”2B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;background-color:black”><img src=”images/blank.gif” name=”3B” id=”3B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;”><img src=”images/blank.gif” name=”4B” id=”4B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;background-color:black”><img src=”images/blank.gif” name=”5B” id=”5B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;”><img src=”images/blank.gif” name=”6B” id=”6B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;background-color:black”><img src=”images/blank.gif” name=”7B” id=”7B” onClick=”javascript:setId(this.id)”></td>
<td style=”width:75px;height:75px;”><img src=”images/blank.gif” name=”8B” id=”8B” onClick=”javascript:setId(this.id)”></td>
</tr>
[/code]

all the images exist and the table does too, the setId function works
but another strange thing is that the image id’s and names seem to mix up 😮 …
any idea why this ain’t working?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JPnycDec 22.2005 — I don't understand why you're using a loop if you're setting the image sources individually. Also, get elementById() isn't needed either really. You could use

document.images.
Copy linkTweet thisAlerts:
@X-rayauthorDec 22.2005 — I don't understand why you're using a loop if you're setting the image sources individually. Also, get elementById() isn't needed either really. You could use

document.images.[/QUOTE]

ok, perhaps I should use document images, that solved the problem of images not showing...

but I don't see why it's not wise to use a for loop...

I prefer having less code in my page as it's just as effective

btw: here's the html page:

[URL=http://www.jvdev.be/chess/]http://www.jvdev.be/chess/index.htm[/URL]
Copy linkTweet thisAlerts:
@X-rayauthorDec 22.2005 — ok, now it works, I just forgot to change a id tag

thanx for the images[] advice though, didn't recon its existence
Copy linkTweet thisAlerts:
@JPnycDec 22.2005 — Well if you're using the images object, you don't really need IDs. Just load the img files into an array in the order you want them, and let the loops do the work
Copy linkTweet thisAlerts:
@X-rayauthorDec 22.2005 — hmm, i started coding with the other way in mind...

building chess with javascript does require id's doesn't it ?
Copy linkTweet thisAlerts:
@JPnycDec 22.2005 — Ah ok. I didn't realize what you were trying to do.
×

Success!

Help @X-ray 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,
)...