Activate an alert function when the text inside a div reaches the value of 10

Currently, I am developing a game where once a player reaches 10 strikes, an alert should pop up saying "Game Over". However, the alert function is not working as expected. The StrikeNumber ID corresponds to the DIV that should trigger the alert once it hits the value of 10. This functionality is written in pure JavaScript.

function GameOver(){
if (document.getElementById('StrikeNumber').text === "10"){
    alert("gameOVER");
   }
}

HTML DIVS

<div id="StrikeNumber">
0
</div>
<div id="ScoreNumber">
0
</div>

Answer №1

For individuals utilizing JQuery, the .change() method can be harnessed to incorporate specific logic within its framework.

$("#UpdateNumber").change(function(){
    alert('number has been updated');
});

Answer №2

The text property is not available in the div element. You should instead use the textContent property.

if (document.getElementById('StrikeNumber').textContent === "10"){
    alert("Game Over");
   }

Learn how to retrieve text from a div tag using JavaScript

Answer №3

To handle the absence of any text, utilize either innerHTML or textContent. Your HTML seems to contain whitespace, so implementing trim() would be a more effective approach. Make sure to understand the difference between == and === by visiting this link.

function GameOver(){
if (document.getElementById('StrikeNumber').innerHTML.trim() === "10"){
     alert("gameOVER");
   }
}

Answer №4

Here is a code snippet that may be helpful for you

(function() {

    var x = document.getElementById("StrikeNumber").innerHTML;

    if (x == 10) {
      alert("Game Over !!!");
    }

  }

)();
<div id="StrikeNumber">
  10
</div>
<div id="ScoreNumber">
  0
</div>

Answer №5

It is recommended to utilize innerHTML in order to retrieve the content of a div element.

function EndGame(){
    if (document.getElementById('StrikeNumber').innerHTML === "10"){
        alert("GAME OVER");
   }
}

Additionally, it is crucial to ensure that the EndGame() function is invoked following any update to the div's content.

Answer №6

Here is a suggestion for you to attempt.

setInterval(function(){
  if(div.textContent === '10') {alert('got it!')}
}, 50);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing Adaptive Images in Bootstrap 5 for a Specified Design

I am currently working on a website layout that involves displaying one image per section. My goal is to have the images positioned on either side of the text when viewed on a larger screen, as shown in the example below: https://i.sstatic.net/bms6S.png ...

Streaming HTTP content on a domain secured with HTTPS using HTML5

Is it possible to stream HTTP on an HTTPS domain without triggering browser security errors? By default, the browser tends to block such requests. I rely on the hls.js library for desktop support with .m3u8 files. When I play content directly (on mobile o ...

How can you prevent the browser from prompting to save your email and password when filling out a sign-up form?

I'm currently developing a PHP sign up form, but whenever I click on the sign up button, the browser prompts me to save the email and password. Is there a way to prevent this from happening? ...

Creating a design utilizing HTML columns with a set height and the ability to scroll horizontally

For my android project, I have a requirement to display a view (WebView) that dynamically loads content. The content will consist of <div class="content-item"> tags that are added by JavaScript to <div class="content-holder"> after the page has ...

button bouncing effect when scrolling vertically

Is there a way to create an element that bounces based on the user's scroll direction? For instance, if the user scrolls down, the button should bounce from bottom to top, and if scrolling up, the button should bounce from top to bottom. I have been ...

What is the best way to interact with a checkbox that has a label using Selenium in Node.js?

My HTML document contains multiple checkbox classes with different texts for each checkbox name. Here is a snippet of the HTML code: <div class="col-md-12 syllabus-div-1"> <h4 class="vertical-spacing">Coaching<i class="fa fa-graduation- ...

Using the factory pattern in a Node.js (Express) application

As I delved into the realm of design patterns, I found myself drawn to the factory pattern. However, as I perused through code written by others, I noticed that this particular pattern wasn't as prevalent, especially in newer stacks. Take for example ...

Retrieving the value of a cell in a table row by clicking on a particular column

After clicking on a specific column in a table, I was able to retrieve all row values by using the event and accessing the value with event.currentTarget.cells[4].innerText();. However, I want this functionality to be triggered only when a certain column, ...

Ways to send notifications through Firebase Cloud Messaging (FCM)

I have written a cloud function that listens for document creation in a specific collection within my database Below is the code snippet of the function: const functions = require('firebase-functions'); const admin = require('firebase-admin ...

What is the best way to add portfolio projects into a React application?

I am currently building a portfolio using react. I am having trouble navigating to my projects through localhost:3000. The projects are stored in my includes folder and are not created with react, so it is different from using XAMPP as my server. My file ...

A label in nativescript not displaying a two-digit number

I've encountered a peculiar issue with my user interface. I have a barcode scanner on my phone that scans barcodes and stores them in an observable array (which is functioning correctly). I also have a label that displays the length of the array. When ...

Could this be an unattended 'error' occurrence?

After incorporating dishRouter.js, promoRouter.js, and leaderRouter.js into app.js, I am encountering the following error message error image This snippet showcases my app.js file: var express = require('express'); var path = require('pat ...

Ensuring the Accuracy of POST Parameters Using Express-Validator

I've been working on adding parameter validation to my Node/Express API by utilizing express-validator. However, I encountered a situation where even though I sent a POST request with a missing "name" field using the curl command curl -X POST -d "foo= ...

What is the method for updating the inner html of an image tag in HTML?

Displayed below is the current output: <img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" /> In order to change it, I must replace src with data: <img width="1080" height="1080" data="/image.jpg" class="post-image" ...

Using jQuery to swap an image with a div element and then using the image as the

I have a collection of images in a div, like so: <div class="image-container"> <img width="174" height="174" src="http://mysite.com/images/portfolio/p1.jpg" class="image-style" typeof="foaf:Image"> <img width="174" height="174" src= ...

Having trouble retrieving the default text from an input element using Selenium

Seeking the date 11/30/2022 from the SOA Handled Date/Time field on a private site shown here. The text is within an input field that is pre-filled when the page loads, and the corresponding HTML looks like this. <td> <input type="tex ...

Tips on stopping Bootstrap Modal from opening when element within trigger is clicked?

I've configured my table rows to trigger a bootstrap modal upon click. However, I have an issue with the select box in each row allowing user selection of status options. Is there a way to prevent the modal from opening when the select box is clic ...

There seems to be an issue with the functionality of Bootstrap 4

I'm having trouble incorporating bootstrap4 into my website because the navbar feature is not functioning properly. It seems like all the attributes are being crossed out in the CSS file. Can someone assist me with this? Here is the result: https://i ...

Identifying the Occurrence of a Partial Insertion in Rails through JavaScript/jQuery

Is there a way to determine if a partial is currently visible using JavaScript? Let's simplify this with some pseudo-code to explain my question - In this scenario, in my controller: def index if (request.xhr?)//coming from pagination ajax requ ...

Discovering common time intervals without the date component in between

I need to identify the intersection between two time ranges, focusing solely on the time and not the date. For instance, range_1 is from 9AM to 6PM while range_2 spans from 5PM to 8AM. The times are in the 24-hour format. I have a solution that finds the o ...