0

Take a look at my jsfiddle first. Now, I want to create a counter everytime Scissor, Rock and Paper appears in the div #myChoice. I have to do it like this.. so no other ways like on click whatever please.

How to do this?

 if "Rock" appears in the div #myChoice
 -> Rock Count: 1
 -> Paper Count: 0
 -> Scissor Count: 0

 if "Scissor" appears in the div #myChoice
 -> Rock Count: 1
 -> Paper Count: 0
 -> Scissor Count: 1

 if "Rock" appears in the div #myChoice AGAIN
 -> Rock Count: 2
 -> Paper Count: 0
 -> Scissor Count: 1

Thanks for your help & sorry for my latest question that nobody understood lol

Syno
  • 1,056
  • 10
  • 25

5 Answers5

2

Is this what you want ?

var helloLength = $('#container:contains("Hello")').length;
if(helloLength >= 1) {
 $("#counter").text("Hello " + helloLength + " times");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
 <div id="container"> Hello </div>
 <div id="counter"> Hello: /*here the number*/ times </div>

If you want to count the number of hello, you should use class instead of id: like this:

var helloLength = $('.container:contains("Hello")').length;
if(helloLength >= 1) {
 $("#counter").text("Hello " + helloLength + " times");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container"> Hello </div> 
 <div class="container"> Hello </div>
 <div class="container"> Random div </div> 
 <div class="container"> Hello </div>
 <div class="container"> Hello </div>

 <div id="counter"> Hello: /*here the number*/ times </div>
Dipiks
  • 3,818
  • 2
  • 23
  • 39
  • Without seeing your code I can't help you with your game. But if you want to count the number of div with Hello (or whatever you want) you can use my answer. If you want a more detailed answer, you should add more details to your question – Dipiks Jan 05 '17 at 15:36
  • @LorisVonlanthen You should edit this one instead – Dipiks Jan 05 '17 at 15:43
  • Look at the edit pls – Syno Jan 05 '17 at 16:07
  • That's not an edit, that's a completely different question! I'd suggest you revert the edit and a new question if you want to ask something else. Although note also that your new question is far too broad. Your previous one was fine, it just needed some clarification on exactly what result you wanted to achieve – Rory McCrossan Jan 05 '17 at 16:08
  • @RoryMcCrossan I wanted to ask this but i messed up my latest question completely – Syno Jan 05 '17 at 16:10
  • @RoryMcCrossan and btw i can't create a new question until 90mins – Syno Jan 05 '17 at 16:11
2

Use Event delegation to handle clicks. If the user clicks on an option, then look for an associated count element and update the count. The example below uses .hasClass() to see if the element clicked on has a class name option (added to distinguish the options from other elements), parseInt() to check for a number in the count container and then isNaN() to check if the count number is actually a number (unlike an empty string).

$(document).ready(function() {
  $(document).click(function(event) {
    if ($(event.target).hasClass('option')) {
      $('#myChoice').text(event.target.innerText);
      var countElement = $('#' + event.target.id + 'Count');
      if (countElement.length) {
        var count = parseInt(countElement.text(), 10);
        if (isNaN(count)) {
          count = 0;
        }
        countElement.text(++count);
      }
    }
  });
});
#myChoice {
  width: 100px;
  height: 20px;
  border: 1px solid black
}
li {
  width: 50px;
  border: 1px solid black;
  padding: 3px;
  margin-bottom: 5px
}
li:hover {
  background-color: gainsboro;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>
    <div id="scissor" class="option">Scissor</div>
  </li>
  <li>
    <div id="rock" class="option">Rock</div>
  </li>
  <li>
    <div id="paper" class="option">Paper</div>
  </li>
</ul>
<br />
<div id="myChoice">
</div>
<br />

<div id="counter">
  <p>Scissor Count:
    <span id="scissorCount"></span>
  </p>
  <p>Rock Count:
    <span id="rockCount"></span>
  </p>
  <p>Paper Count:
    <span id="paperCount"></span>
  </p>
</div>
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
  • Sorry that i'm this late.. was in hospital lol.. thanks tho – Syno Jan 16 '17 at 09:21
  • i have no idea why the question got closed.. I'm so thankful u responded in time – Syno Jan 16 '17 at 09:31
  • It was likely closed because your question didn't include "a _specific problem or error_" - it did have a link to the jsFiddle and a question "_How to do this?_" but that isn't a very *specific* question and likely won't help other people who happen to view your question. If it was a specific question like [this one](http://stackoverflow.com/questions/6736476/javascript-parseint-return-nan-for-empty-string) then it likely wouldn't have been closed – Sᴀᴍ Onᴇᴌᴀ Jan 16 '17 at 15:47
  • It is indeed a specific question, it even has an example? – Syno Jan 16 '17 at 15:51
  • I should have been more clear: Your post does not have "_a clear problem statement_" and thus is not really useful to other readers. From the [What topics can I ask page](http://stackoverflow.com/help/on-topic) - "if your question generally covers ... **a specific programming problem**, or ...then you’re in the right place to ask your question!" However, "_How to do this?_" doesn't cover a **specific** programming problem. – Sᴀᴍ Onᴇᴌᴀ Jan 16 '17 at 16:48
1

You can achieve this by using a regular expression and the match() method to find the number of occurrences of 'Hello' within your string. From there you can use text() on a span within the #counter element to show the value. Try this:

var re = /hello/gi;
var count = ($('#container').text().match(re) || []).length;

$('#counter .count').text(count);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"> Hello hello </div>
<div id="counter"> Hello: <span class="count"></span> times </div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

I just want to count +1 inside of a div everytime the other div contains the word Hello.

I took this to mean you want to count the number of times the word "Hello" is in the first div, so here's a simple loop to do that.

// create an array out of the text of #container using spaces to delimit
var text = $('#container').text().split(" ");
var count = 0;

// loop through the array of text and check to see if each word is "Hello"
for (var i = 0; i < text.length; i++) {
  if (text[i] === "Hello") {
    count++;
  }
}  

$('#counter').text("Hello: " + count + " times");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="container">Hello Hello Hello Hello</div>
<div id="counter"></div>
Jon Uleis
  • 17,693
  • 2
  • 33
  • 42
0

You can just use this simple function countWords()

function countWords(str){return (str.match(/Hello/g) || []).length;;}
$('#counterValue').text( countWords($("#container").text()) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<div id="container"> Hello and Hello but Hello is what I'm saying... last Hello!</div>
<div id="counter"> Hello: <span id="counterValue"></span> times </div>
Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65