Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

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 07-18-2011, 07:00 PM   PM User | #1
DeathCamel57
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
DeathCamel57 is an unknown quantity at this point
image src changing

i want to be able to click a button then my image's src will be changed
each image filename is like 1.jpg 2.jpg 3.jpg... this function should add 1 to the number before the ".jpg"

my code so far is:
Code:
<script type="text/javascript" >
 
var a = document.getElementById('img').src;
var b = var a + 1

function right()
{
document.getElementById('img').src="'img/'+'var b'+'.jpg'";
 }
</script>
my image has src="1.jpg" by default
DeathCamel57 is offline   Reply With Quote
Old 07-18-2011, 08:06 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
oh this is weird code. You should learn how to handle javascript variables. The "var" keyword is used for declaring a variable not for every access to it.
Code:
"'img/'+'var b'+'.jpg'"
is a literal string. Every single character of this string will be assigned to .src of the image. This is clearly not what you wanted to do

Try this instead
Code:
"img/" + b + ".jpg"
Then you are reading a string from the image .src attribute. You want to add 1 to this string? Clearly not. You want to extract the current number (or store it somewhere) and increase it by 1. You'll have to rethink your code ....
devnull69 is offline   Reply With Quote
Old 07-18-2011, 08:42 PM   PM User | #3
DeathCamel57
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
DeathCamel57 is an unknown quantity at this point
Could you tell me how to extract the number
how would i use parseFloat and parseInt to do this?
DeathCamel57 is offline   Reply With Quote
Old 07-18-2011, 08:55 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Code:
var a = document.getElementById('img').src.match(/(\d+)/)[1];
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
DeathCamel57 (07-18-2011)
Reply

Bookmarks

Tags
easy, function, image, javascript, src

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:07 AM.


Advertisement
Log in to turn off these ads.