Question

<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <title>Vote!</title>
    <script type="text/javascript">
    var x1 = 0;
    function countClicks() {
        x1 += 1
        document.getElementById( "counting" ).innerHTML = x1;
       ClickCount++;
       return true;
    }
    </script>

    <script type="text/javascript">
    var x2 = 0;
    function countClicks1() {
        x2 += 1
        document.getElementById( "counting1" ).innerHTML = x2;
        ClickCount++;
        return true;
    } 
    </script>

    <script type="text/javascript">
    var x3 = 0;
    function countClicks2() {
        x3 += 1
        document.getElementById( "counting2" ).innerHTML = x3;
        ClickCount++;
        return true;
    }
    </script>
</head>
<body>
 <div id="chart1">
 <ul>
  <li>
   <img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br/>
   <input type="button" value="VOTE" name="clickOnce" onclick="return countClicks();" />
   <div id="counting"></div>
  </li>
  <li>
   <img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br/>
   <input type="button" value="VOTE" name="clickOnce" onclick="return countClicks1();" />
   <div id="counting1"></div>
  </li>
  <li>
   <img src="../Pictures/BWS + L.A +KUSH/Game.RED_Album_Cover.jpg" alt="red album"><br>
   <input type="button" value="VOTE" name="clickOnce" onclick="return countClicks2();" />
   <div id="counting2"></div>
  </li>
 </ul>
</div>
</body>
</html>
Was it helpful?

Solution

Of course . Pass your div id as an argument to the function . See http://jsfiddle.net/KsePr/ for a demo .

One word of caution - If you are trying to implement some kind of voting system on a webpage , you'll need to do the vote incrementing in at the server side , not the client side .

OTHER TIPS

Try this:

var countMap = {};
function countClicks(id) {
    countMap[id] = countMap[id] + 1 || 1;
    document.getElementById(id).innerHTML = countMap[id];
}

Now you can call it with any element id you want, like:

countClicks('counting');
countClicks('counting1');
countClicks('counting2');

Note that this will work with any number of elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top