468,770 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,770 developers. It's quick & easy.

Changing a number with onClick

ok so i have the code listed below and i want it so that when i click the button x goes up by 60 for every click. then, this part i need but not as much, when it reaches y i would like it to execute a command to change a pic.
Expand|Select|Wrap|Line Numbers
  1. <input name="submit" type="button" value="button" onclick="button" />
  2. <script language="JavaScript" type="text/javascript">
  3.  
  4. var x;
  5. var y;
  6.                                                             x = 0;
  7. y = 600;
  8.  
  9. document.write(x);
  10. document.write("/");
  11. document.write(y);
  12.  
  13. </script>
Nov 15 '11 #1
9 2246
Dormilich
8,651 Expert Mod 8TB
first of all, document.write() doesn’t work with(in) events.
Expand|Select|Wrap|Line Numbers
  1. <input id="submit" type="button" value="button">
  2. <input id="display" type="text" readonly>
Expand|Select|Wrap|Line Numbers
  1. function changePicOrNot()
  2. {
  3.     if (60 == this.clicked++) {
  4.         changeSRC();
  5.     }
  6.     display.value = this.clicked + "/60";
  7. }
  8. var display = document.getElementById("display");
  9. var submit  = document.getElementById("submit");
  10. submit.clicked = 0;
  11. // all browsers except IE < 9
  12. submit.addEventListener("click", changePicOrNot, true);
Nov 16 '11 #2
thanks but the pic isnt changing and i want the number to stay there even if i refresh the page.
Nov 16 '11 #3
Rabbit
12,515 Expert Mod 8TB
You have to write a function to change the source of the pic. If you refresh the page, you can't keep the value unless you save it in a cookie or on a server.
Nov 16 '11 #4
well can you tell me that function is then?
Nov 17 '11 #5
Rabbit
12,515 Expert Mod 8TB
I don't give out code unless the poster shows an attempt.
Nov 17 '11 #6
this is what i have.
Expand|Select|Wrap|Line Numbers
  1. <script language="JavaScript">
  2.     function changeSRC() {
  3.         document.getElementById("x").src = "pic2.png"
  4.     }
  5.  
  6. </script>
  7. <img id="x" src="pic1.jpg" alt="x" onchange="changeSRC()" />
Nov 17 '11 #7
thanks but my code doesnt seem to work.
Nov 17 '11 #8
Dormilich
8,651 Expert Mod 8TB
I don’t think an image having a change event would make sense …

onchange
User committed a change to the value of element (form control).
Nov 17 '11 #9
it did work i just forget to clear my cache. and your right i dont need the on change as it doesnt do anything for this.
Nov 17 '11 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Ben | last post: by
1 post views Thread by Lars Netzel | last post: by
3 posts views Thread by ICPooreMan | last post: by
20 posts views Thread by Shalini Bhalla | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.