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 08-18-2008, 03:52 PM   PM User | #1
Martin310
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Martin310 is an unknown quantity at this point
onclick image hyperlinks

Im currently putting a website together adn ive come across a problem that im having difficulty solving.

I've got several images in my gallery page, using the javascript, and the onclick function, I have all images appearing in the center of the page when click.

I want to use the center image as a hyperlink, going to a different page depending on whick picture is there.

Im totally inexperienced at javascript, can anybody give me any suggestions?

Thanks
Martin
Martin310 is offline   Reply With Quote
Old 08-18-2008, 04:19 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Could you show us your current code?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 08-18-2008, 04:24 PM   PM User | #3
Martin310
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Martin310 is an unknown quantity at this point
<SCRIPT language="JavaScript">
<!--
if (document.images)
{
Elements_Color= new Image(177,118);
Elements_Color.src="Website_Files/Images/Web_product_images/Elements_Colour.jpg";

Drafting_Color= new Image(177,118);
Drafting_Color.src="Website_Files/Images/Web_product_images/Drafting_Colour.jpg";
}

function change1(picName,imgName)
{
if (document.images)
{
imgOn=eval(imgName + ".src");
document[picName].src= imgOn;
}
}
//-->
</script>
</head>
<body>

<img src="Website_Files/Images/Web_product_images/Drafting_B&W.jpg onclick="change1('MainImage','Drafting_Color')" >

<img src="Website_Files/Images/Web_product_images/Elements_B&W.jpg" onclick="change1('MainImage','Elements_Color')" >

<IMG SRC="Website_Files/Images/Web_product_images/KSC_Colour.jpg"
name="MainImage" width="177" height="118" border="0" valign="center" align="center" >
Martin310 is offline   Reply With Quote
Old 08-19-2008, 05:34 AM   PM User | #4
rangana
Senior Coder

 
rangana's Avatar
 
Join Date: Feb 2008
Location: Cebu City, Philippines
Posts: 1,752
Thanks: 65
Thanked 372 Times in 365 Posts
rangana will become famous soon enoughrangana will become famous soon enough
  • Surround your centered image with <a> tag:
    Code:
    <a href="#">
    <IMG SRC="Website_Files/Images/Web_product_images/KSC_Colour.jpg" 
    name="MainImage" width="177" height="118" border="0" valign="center" align="center">
    </a>
  • Pass the url you want the image be linked to:
    Code:
    <img src="Website_Files/Images/Web_product_images/Drafting_B&W.jpg" onclick="change1('MainImage','Drafting_Color','http://www.google.com')" >
    
    <img src="Website_Files/Images/Web_product_images/Elements_B&W.jpg" onclick="change1('MainImage','Elements_Color','http://www.codingforums.com')" >
  • And modify change1 function into:
    Code:
    function change1(picName,imgName,uri)
    {
    if (document.images)
    {
    imgOn=eval(imgName + ".src");
    document[picName].src= imgOn;
    document[picName].parentNode.setAttribute('href',uri);
    }
    }

P.S. You have a lot of deprecated attributes, have sometime to make it valid.

You might also want to make use of document.getElementById in replacement of document[picName].

Hope that helps.
__________________
Learn how to javascript at 02geek

The more you learn, the more you'll realize there's much more to learn
Ray.ph

Last edited by rangana; 08-19-2008 at 05:36 AM..
rangana is offline   Reply With Quote
Old 08-19-2008, 05:23 PM   PM User | #5
Martin310
New to the CF scene

 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Martin310 is an unknown quantity at this point
Thanks so much for that, it works perfectly.
I was along the right lines when i was trying to sort it, but i missed a few key lines out haha, nevermind.

Thanks again
Martin
Martin310 is offline   Reply With Quote
Reply

Bookmarks

Tags
hyperlink, image, javascript, onclick

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 11:26 PM.


Advertisement
Log in to turn off these ads.