By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,945 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,945 IT Pros & Developers. It's quick & easy.

Changing a number with onClick

P: 6
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
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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

P: 6
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
Expert Mod 10K+
P: 12,430
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

P: 6
well can you tell me that function is then?
Nov 17 '11 #5

Rabbit
Expert Mod 10K+
P: 12,430
I don't give out code unless the poster shows an attempt.
Nov 17 '11 #6

P: 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

P: 6
thanks but my code doesnt seem to work.
Nov 17 '11 #8

Dormilich
Expert Mod 5K+
P: 8,639
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

P: 6
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.