identify the image that is currently visible on the screen

I am struggling to identify the image currently displayed onscreen for a matching game. Despite trying various variables such as img and images[], I keep receiving undefined in the console log. Any suggestions on how to determine which image is being shown? Thanks.

Furthermore, this is a typescript application that involves some JavaScript code

dashboard.ts

 ngOnInit() {


 //function to randomize and retrieve face images
    var w = document.getElementById('wrapper');
    var button = document.getElementById('randomize');
    var images = w.children; // inner elements, your image divs
    // a function to hide all divs
    var hideDivs = function(imgs: HTMLCollection) {
      for (var img of < any > imgs) {
        (img as HTMLElement).style.display = 'none';

      }
    }
    hideDivs(images); // initially hide all

    button.addEventListener('click', function(event) {
      var rnd = Math.floor(Math.random() * images.length); // get random index
      hideDivs(images); // hide all images

      (images[rnd] as HTMLElement).style.display = 'block'; // show random image
      (event.target as HTMLElement).textContent = 'Click one more time!';
      console.log("NEED THE OUTPUT OF CURRENTLY DISPLAYED IMAGE HERE:" + );
    })



   }

dashboard.html

   <div id="wrapper" align="center">
  <img class="happy" img src='../../assets/happy.PNG' alt="happy" img id="happy"> 
  <img class="sad" img src='../../assets/sad.PNG' alt="sad" img id="sad">
  <img class="embarrassed" img src='../../assets/embarrassed.PNG' alt="embarrassed" img id="embarrassed">
  <img class="sleepy" img src='../../assets/sleepy.PNG' alt="sleepy" img id="sleepy">
  <img class="stressed" img src='../../assets/stressed.PNG' alt="stressed" img id="stressed">
  <img class="suprised" img src='../../assets/suprised.PNG' alt="suprised" img id="suprised">
  <img class="excited" img src='../../assets/excited.PNG' alt="excited" img id="excited">


</div>
<button id='randomize' onClick = "this.style.visibility= 'hidden';" class="btn btn-success btn-block">START GAME</button>

Answer №1

If you want to extract the images being shown, follow these steps:

 button.addEventListener('click', function(event) {
      ...
      var image = (images[rnd] as HTMLImageElement);
      console.log('src', image.src);
      console.log('class', image.className);
    })

Answer №2

Attempt to find pictures within the ngAfterViewInit() lifecycle hook.

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

Sending information with JSON using POST request through AJAX with PHP

Hello everyone! I have encountered a problem with my JavaScript/jQuery code and the way PHP is handling JSON input as an array. In my rate.php file, I am using print_r($_POST) to display the data, but it seems like PHP is not recognizing the JSON input cor ...

Transmitting HTML markup code

Let's discuss an example with a form that may not be secure or correct: <BODY> <FORM ACTION="" METHOD="post" NAME="test" ID="test"> <INPUT TYPE="text" NAME="testt" VALUE="1"> <INPUT TYPE="reset" VALUE="testtt" onClick="test.s ...

Discovering a secondary identification for an HTML element that contains multiple IDs

I have a jsp page in my project with the following html structure: <table id="hor-minimalist-a" class="campos"> <thead> <tr> <th>Campo</th> <th>#</th> </tr> </thead> <tfoot ...

Utilizing javascript/jquery to extract information from a table

Greetings! As someone who is new to JavaScript and jQuery, I am curious if there is a way to select table data using the following format: $("#myTable").row[index1][index2]; In this format, index1 represents the row index, while index2 represents the col ...

Discovering the initial word of a string using jQuery

I'm currently facing an issue with jQuery that I need help solving. Here's the scenario: I am creating tooltips and positioning them directly under a specific trigger using CSS. Everything is functioning correctly, but there is one problem: In ...

What is the best way to eliminate several values from an array in javascript?

var array = [{ name: "Mango", weight: "15gm" }, { name: "Banana", weight: "10gm" }, { name: "Apple", weight: "15gm" }, { name: "Grapes", weight: "5gm" }, { name: "Banana", weight: "15gm" }]; I need to filter out eve ...

Mongoose and BlueBird emerge victorious from fulfilling their promise

When using mongoose and bluebird as a promise framework, I encounter an error whenever I use "save" or "remove": Warning: a promise was created in a handler but was not returned from it I have spent days trying to resolve this issue. I have tried various ...

Assuring and receiving a response object as null when using the .then method

Working with json data retrieved from a web service myService.getData() .then(function(data, status, headers, config){ alert(data.length); }... While I can successfully retrieve and inspect data through the browser console wit ...

A Kendo editor necessitates the use of Kendo JavaScript specifically designed for Angular

Can someone provide guidance on the necessary Kendo libraries for implementing the Kendo editor in AngularJS? The tutorial site suggests that "kendo.all.min.js" is required, but I am hesitant to use it due to its resource-heavy nature. Any assistance wou ...

Is there a way for me to create a clickable link from a specific search result retrieved from a MySQL database using an AJAX

Currently, I am attempting to create an ajax dropdown search form that provides suggestions based on results from a MySQL database. The goal is for the user to be able to click on a suggestion and be redirected to the specific product. The code I am using ...

Spacing the keyboard and input field in React Native

Is there a way to add margin between the input and keyboard so that the bottom border is visible? Also, how can I change the color of the blinking cursor in the input field? This is incorrect https://i.sstatic.net/Jsbqi.jpg The keyboard is hidden https: ...

Error: html2canvas encountered an uncaught undefined promise

Currently, I am working with an HTML canvas tag: <canvas id="myCanvas"></canvas> I have successfully created a drawing on this canvas, and it looks exactly how I wanted it to. However, when trying to convert it to a PNG file using html2canvas ...

Using JavaScript to redirect to a different page while passing parameters

I've been experimenting with passing parameters from one ASP.NET page to another within our website by using JavaScript, jQuery, Ajax, Fetch, etc., and then capturing this parameter on the Load event of the redirected page using JavaScript. I'm u ...

Is it possible to set custom spacing for specific breakpoints in a Material-ui theme?

Currently, I am in the process of ensuring that my Material-ui + React dashboard is responsive and well-styled for various screen sizes. Within my custom theme, I have set the spacing to 8: import { createMuiTheme } from '@material-ui/core/styles&ap ...

What is the best way to ensure that a JavaScript function is executed continuously for each value in a PHP array?

I am facing a challenge with designing an auction website. My goal is to update the status of specific auctions in a SQL database to 'Closed' once their ending time has passed. However, I'm unsure about how to efficiently run a JavaScript fu ...

Having trouble retrieving the default selected value using the index in Angular Material's mat-button-toggle functionality

I'm encountering an issue with setting the default value for a toggle button group. The code is simple and the toggle buttons are correctly fetching values from the index, but I can't seem to get one of them to be default selected. I tried settin ...

Transform Euler Angles into Vectors by utilizing Euler's "setFromVector3" method

Given three Euler angles x, y, z in radians, I am looking to convert them into a Vector location X, Y, Z with the center as the origin. If it is possible to utilize https://threejs.org/docs/#api/en/math/Euler.toVector3 to obtain the Vector, I would apprec ...

The React component fails to inherit any props that are passed to it when it is rendered using a logical operator

I'm facing an issue with a component that doesn't seem to receive any props when I use a logical operator in conjunction with it. Oddly enough, if I render the component without the conditional statement, everything works fine. But as soon as I a ...

Browser page caching is a way for web browsers to store copies

I am currently investigating how Internet Explorer caches page content (such as input and textarea) in the browsing history. Steps taken: User visits Page1 with a textarea, then navigates to Page2, and returns to Page1 where the textarea data is automati ...

The NestJs provider fails to inject and throws an error stating that this.iGalleryRepository.addImage is not a recognized function

I'm currently working on developing a NestJS TypeScript backend application that interacts with MySQL as its database, following the principles of clean architecture. My implementation includes JWT and Authorization features. However, I seem to be enc ...