Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems getting 'onClick' to work via function

Status
Not open for further replies.

SLaut

Technical User
Jan 19, 2006
3
FI
I've recently started learning JavaScript to get a portfolio done and run into a problem I just can't solve...

I have a bunch of thumbnails whose linked images and their links (with pop-up windows) should change along what section the viewer's at. I've gotten the pictures to work perfectly with a function that changes them with the document.getElementById('x').src="x.jpg" bit but for some reason, when I get a link by its id and change the onClick value (the code I'm using is document.getElementById('kuva2').onClick="javascript:window.open('digital-paintings/1.htm','dip1','toolbar=0,scrollbars=0,width=850,height=700')"; ), nothing happens. By typing javascript: to Mozilla's address box I get no errors though.

In case it's necessary, here's one image's slice of the function with the linked image code it refers to:

function digitalpaintings () {
document.getElementById('kuva1').src="thumbnails/dip1.jpg";
document.getElementById('linkki1').onClick="javascript:window.open('digital-paintings/1.htm','dip1','toolbar=0,scrollbars=0,width=850,height=700')";
}
...
<a href="#" id="linkki1" onClick=""><img id="kuva1" src="layout/spacer.gif" width="100" height="100" style="position:absolute; left:0; top:5;" border="0"></a>
 
I can see why you would think that would work... but like a few other attributes (the "style" attribute for example), while they are set in HTML as strings, setting them in JS sometimes requires you to do things a bit differently.

If you change this:

Code:
document.getElementById('linkki1').onClick="javascript:window.open('digital-paintings/1.htm', 'dip1', 'toolbar=0,scrollbars=0,width=850,height=700')";

to this:

Code:
document.getElementById('linkki1').onclick = function() {
	window.open('digital-paintings/1.htm', 'dip1', 'toolbar=0,scrollbars=0,width=850,height=700');
}

You should be home and dry.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Awesome, it works like a dream! :D

Thanks a lot!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top