How to add counter based tag in Javascript

I am making a simple game in HTML / Javascript where every time a button or hyperlink is clicked, it adds 1 to the counter

<script type="text/javascript">
var count = 0;
function countClicks() {
 count = count + 1;
    document.getElementById("p2").innerHTML = count;
}
</script>

My question is, is there a way to add a checkmark (Ascii # 10004) equal to the number in the counter. I'm sure this is easy to solve, but I have never used Javascript, and it seems to be the easiest language to do this. I appreciate any help provided

+4
source share
6 answers

You can use HTML decimal: &#9745;

Just replace the code:

document.getElementById("p2").innerHTML = count;

with the following code:

document.getElementById("p2").innerHTML = "&#9745 "+count;

Or you can use:

document.getElementById("p2").innerHTML = "&#10004 "+count;

The result will be like this:

✔ 5

here 5 is your score.

+2
source

, . .

. jsfiddle

var count = 5; // count is already 5 for demo purpose
function countClicks() {
    count = count + 1;
    var ticks = Array(count+1).join("&#10004;");
    document.getElementById("p2").innerHTML = count+' '+ticks;
}

countClicks(); # 6 ✔✔✔✔✔✔
+2

, String.fromCharCode ascii- . , count:

<script type="text/javascript">
var count = 0;
function countClicks() {
    count = count + 1;
    var res = String.fromCharCode(10004);
    var output = "";
    for (var i = 0; i < count; i++) {
       output += res+" ";
    }
    document.getElementById("p2").innerHTML = output;
}
</script>
0

, , ? :

var count = 0;
function countClicks() {
 count = count + 1;
 document.getElementById("p2").innerHTML = document.getElementById("p2").innerHTML + '&#10004';
}

, :

var p2, count = 0;
function countClicks() {
 count = count + 1;
 p2 = document.getElementById("p2");

 p2.innerHTML = p2.innerHTML + '&#10004';
}

, - .

0

:

http://jsfiddle.net/oL83m567/1/

var count = 0;

function countClicks() {
  count = count + 1;
  var tick='';
  for(var i=0;i<count;i++)
    tick+= '&#x2714';
  document.getElementById("p2").innerHTML = count + tick;
}
0

. , , , , . , . - :

<script type="text/javascript">
var ticks=new Array();
function countClicks() {
     ticks[ticks.length]="&#10004;";
     document.getElementById("p2").innerHTML = ticks.join("");
}
</script>
0

All Articles