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 11-09-2012, 03:31 PM   PM User | #1
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Trying to change pic from link with onclick

I'm having trouble using the onclick. What im trying to do is by clicking a link i want it to change a picture. I've tried using the onclick with the getElementById and i've also tried to send it pack to a function. Also after the link is clicked I need it to change to say view smaller image and I need it to display the smaller one when it is clicked. Would I do this by using the innerHTML? Any help is appreciated thank you.

Code:
<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript"> //function isn't active not sure if it works
var img1 = cottage_small.jpg;
var img2 = cottage_large.jpg;
var choice = 1;

function change(){
if (choice == 1){
document.getElementById('pic').src="img2";
choice = 2;
}else if(choice == 2){
document.getElementById('pic').src="img1";
choice = 1;
}
}
</script>
</head>
<body>
<h1>Pine Knoll Properties</h1>
<br>
<h2>Single Family Hours</h2>
<br>
<p>Cottage: <b>$149,00</b><br>
2 bed, i bath, 1,189 square feet, 1.11 acres
<br>
<br>
<a id="large" href="" onclick="document.getElementById('pic').src='cottage_large.jpg';">
View Larger image</a>
<br>
<br>
<img id="pic" alt="" src="cottage_small.jpg">
</p>

</body>
</html>

Once again thanks.
CodyJava is offline   Reply With Quote
Old 11-09-2012, 04:18 PM   PM User | #2
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
Code:
var img1 = cottage_small.jpg;
var img2 = cottage_large.jpg;
should be

Code:
var img1 = "cottage_small.jpg";
var img2 = "cottage_large.jpg";


Code:
document.getElementById('pic').src="img2";
document.getElementById('pic').src="img1";
should be
Code:
document.getElementById('pic').src=img2;
document.getElementById('pic').src=img1;
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
CodyJava (11-09-2012)
Old 11-09-2012, 04:45 PM   PM User | #3
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Thank you do you know how I would change the links. Originally it says larger image and after click i want it to say smaller image. Would I use innerHTML thanks again.
CodyJava is offline   Reply With Quote
Old 11-09-2012, 06:50 PM   PM User | #4
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Update

I have updated my code but still nothing. Any help is appreciated.

Code:
<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var img1 = "cottage_small.jpg";
var img2 = "cottage_large.jpg";
var choice = 1;

function change(){
if (choice == 1){
document.getElementById('pic').src=img2;
choice = 2;
}else if(choice == 2){
document.getElementById('pic').src=img1;
choice = 1;
}
}
</script>
</head>
<body>
<h1>Pine Knoll Properties</h1>
<br>
<h2>Single Family Hours</h2>
<br>
<p>Cottage: <b>$149,00</b><br>
2 bed, i bath, 1,189 square feet, 1.11 acres
<br>
<br>
<a id="large" href="" onclick="change();">
View Larger image</a>
<br>
<br>
<img id="pic" alt="" src="cottage_small.jpg">
</p>

</body>
</html>
CodyJava is offline   Reply With Quote
Old 11-09-2012, 06:56 PM   PM User | #5
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,335
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
Code:
function change(el){
var myPic = document.getElementById('pic');
if (choice == 1){
myPic.src="img2";
el.innerHTML = "Small"
choice = 2;
}else if(choice == 2){
myPic.src="img1";
el.innerHTML = "Large"
choice = 1;
}
}


<a id="large" href="" onclick="change(this);">
i think this will do it.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
CodyJava (11-09-2012)
Old 11-09-2012, 09:30 PM   PM User | #6
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
I know this is aggravating but here's the code now it's still not working in fact its opening up windows explorer and going to the file when i use IE when I use chrome it just does nothing. Can you explain thanks.


<!DOCTYPE HTML>
<html>
<head>
<title>Chapter 11</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script type="text/javascript">
var choice = 1;

function change(el){ //is el element?
var myPic = document.getElementById('pic');
if (choice == 1){
myPic.src=cottage_large.jpg; //do I need to change the id in img to myPic??
el.innerHTML = "Small"
choice = 2;
}else if(choice == 2){
myPic.src=cottage_small.jpg;
el.innerHTML = "Large"
choice = 1;
}
}

</script>
</head>
<body>
<h1>Pine Knoll Properties</h1>
<br>
<h2>Single Family Hours</h2>
<br>
<p>Cottage: <b>$149,00</b><br>
2 bed, i bath, 1,189 square feet, 1.11 acres
<br>
<br>
<a id="large" href="" onclick="change(this);">
View Larger image</a>
<br>
<br>
<img id="pic" alt="" src="cottage_small.jpg">
</p>

</body>
</html>

Thanks I'm not good at js at all.
CodyJava is offline   Reply With Quote
Old 11-09-2012, 09:37 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If you do not return false from the onclick in an <a> tag, then the NORMAL ACTION of the <a> tag *WILL* take place. Meaning that the browser will ask the server for the page specified by the href and load it, wiping out the current page.

In your <a>, the href is blank, which the browser interprets to mean "this same page". So it *WILL* go back to the server to get this page again, effectively wiping out the change to the image that you made.

The answer is simple:
Code:
<a id="large" href="" 
  onclick="document.getElementById('pic').src='cottage_large.jpg'; return false;">
(And DanInMa knows that...he just missed it in the code.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
CodyJava (11-09-2012)
Old 11-09-2012, 09:47 PM   PM User | #8
CodyJava
New Coder

 
Join Date: Sep 2012
Posts: 25
Thanks: 21
Thanked 0 Times in 0 Posts
CodyJava is an unknown quantity at this point
Thanks I think i need to send the onclick back to a function though because I need to use the same <a> tag to change it from 1 picture to the other. Also change the text that the link says. Thats why i was trying to use a function. Doesn't just referencing to the 1 picture when using the onclick bypass all that?
CodyJava is offline   Reply With Quote
Old 11-09-2012, 10:55 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Two ways:

First:
Code:
<a onclick="whatever( ); return false;">...</a>
Second:
Code:
function whatever( ) 
{
    ... change images, etc. ...
   return false;
}

and then

<a onclick="return whatever( );">...</a>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
CodyJava (11-09-2012)
Old 11-09-2012, 10:56 PM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,055 Times in 4,024 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The point is that you SOMEHOW want to ensure the onclick returns false. It doesn't matter how.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
The Following 2 Users Say Thank You to Old Pedant For This Useful Post:
CodyJava (11-09-2012), DanInMa (11-11-2012)
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 11:54 AM.


Advertisement
Log in to turn off these ads.