Bild durch Klick in DIV befördern / Counter erhöhen

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Bild durch Klick in DIV befördern / Counter erhöhen

    Ich glaube ich brauche mal ne kleine Hilfe da ich mit Javascript noch nichts wirkliches am Hut hatte...

    Also ich bastele da grade was und hab 4 Div Boxen. Sieht so aus!

    Quellcode

    1. <div id="tresorcode_num1" style="width:101px; height:101px; border:#666666 solid 1px;" align="center"></div>


    Und davon halt 4...

    Jetzt hab ich bzw. möchte ich das man wenn man auf eine Zahl klickt diese in dem div steht also der erste klick in div tresorcode_num1 der zweite in div tresorcode_num1 usw...

    Da hab ich folgendes probiert: (Hab schon gehört das es Müll ist)



    Quellcode

    1. var counter = 0;
    2. function update_counter(){
    3. var counter = counter + 1;
    4. }
    5. function code(zahl){
    6. if(counter == 0) {
    7. document.getElementById("tresorcode_num1").innerHTML = "<img src=\"./img/buttons/" + zahl + ".png\">";
    8. update_counter();
    9. }
    10. if(counter == 1) {
    11. document.getElementById("tresorcode_num2").innerHTML = "<img src=\"./img/buttons/" + zahl + ".png\">";
    12. update_counter();
    13. }
    14. if(counter == 2) {
    15. document.getElementById("tresorcode_num3").innerHTML = "<img src=\"./img/buttons/" + zahl + ".png\">";
    16. update_counter();
    17. }
    18. if(counter == 3) {
    19. document.getElementById("tresorcode_num4").innerHTML = "<img src=\"./img/buttons/" + zahl + ".png\">";
    20. update_counter();
    21. }
    22. }
    Alles anzeigen


    Geht natürlich nicht.
    Ich hab natürlich grob im Hirn wie es gehen soll:

    Also der User klickt sagen wir mal auf 5 die 5 wird in das erste DIV geschrieben. Counter wird um 1 erhöht. Und das geht dann bis zum 4.
    Nebenbei sollen die geklickten Zahlen noch in ein Formular geschrieben werden welches nach einem Klick also wenn der Counter auf 4 steht abgeschickt wird...

    Ich hoffe ich bin wenigstens nahe dran & bitte um Hilfe! :lol:
  • Quellcode

    1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    2. <html><head>
    3. <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    4. <style type="text/css">
    5. <!--
    6. .rechner {
    7. width:60px;
    8. }
    9. .rechner div {
    10. border:1px solid #afafaf;
    11. width:15px;
    12. float:left;
    13. }
    14. .rechner div:hover {
    15. background-color:#efefef;
    16. }
    17. //-->
    18. </style>
    19. <script type="text/javascript">
    20. <!--
    21. var counter = 0;
    22. function update_counter(){
    23. counter = counter + 1;
    24. }
    25. function code(zahl){
    26. if(counter == 0) {
    27. document.getElementById("tresorcode_num1").innerHTML = zahl;
    28. document.getElementById("zahlen").value += zahl;
    29. update_counter();
    30. }
    31. else if(counter == 1) {
    32. document.getElementById("tresorcode_num2").innerHTML = zahl;
    33. document.getElementById("zahlen").value += zahl;
    34. update_counter();
    35. }
    36. else if(counter == 2) {
    37. document.getElementById("tresorcode_num3").innerHTML = zahl;
    38. document.getElementById("zahlen").value += zahl;
    39. update_counter();
    40. }
    41. else if(counter == 3) {
    42. document.getElementById("tresorcode_num4").innerHTML = zahl;
    43. document.getElementById("zahlen").value += zahl;
    44. update_counter();
    45. document.forms[0].submit();
    46. }
    47. }
    48. -->
    49. </script>
    50. <title>Test</title>
    51. </head><body>
    52. <div class="rechner">
    53. <div onclick="code(1)">1</div>
    54. <div onclick="code(2)">2</div>
    55. <div onclick="code(3)">3</div>
    56. <div onclick="code(4)">4</div>
    57. <div onclick="code(5)">5</div>
    58. <div onclick="code(6)">6</div>
    59. <div onclick="code(7)">7</div>
    60. <div onclick="code(8)">8</div>
    61. <div onclick="code(9)">9</div>
    62. <div onclick="code(0)">0</div>
    63. </div>
    64. <div style="clear: both" />&nbsp;</div>
    65. <div id="tresorcode_num1" style="width:101px; height:101px; border:#666666 solid 1px;text-align:center"></div>
    66. <div id="tresorcode_num2" style="width:101px; height:101px; border:#666666 solid 1px;text-align:center"></div>
    67. <div id="tresorcode_num3" style="width:101px; height:101px; border:#666666 solid 1px;text-align:center"></div>
    68. <div id="tresorcode_num4" style="width:101px; height:101px; border:#666666 solid 1px;text-align:center"></div>
    69. <form method="post">
    70. <input type="hidden" name="zahlen" id="zahlen" readonly="readonly" />
    71. </form>
    72. </body>
    73. </html>
    Alles anzeigen