Create a button toggle feature to dynamically display data for a specific record within an ng-repeat loop

Currently, I am facing an issue while implementing a start and stop timer in JavaScript for a list of records. To display all the items, I am using ng-repeat. Each repeated element has a Start/Stop toggle button. The problem arises when there are 4 records - if I click on the start timer button for the 4th record, then the button for the first record also changes to Stop. No matter which button I click, it always toggles the first record's button.

Below is the HTML code snippet:

<ul class="list-group" data-ng-repeat="job in 
allAppliedJobsForTimeTracker">
<li class="list-group-item">{{job.title}} Hours : {{job.hours}}
<input type="button" name="btn" id='btn' value="Start" data-ng-
click="startTimeTracker(job)" onclick="to_start()";> </li>
</ul>

And here is the script code:

<script language=javascript>
var h=0;
var m=0;
var s=0;

function to_start(){
    switch(document.getElementById('btn').value){
        case 'Stop':
            window.clearInterval(tm); // Stop the timer
            document.getElementById('btn').value='Start';
            break;
        case 'Start':
            tm = window.setInterval('disp()', 1000);
            document.getElementById('btn').value='Stop';
            break;
    }
}

function disp(){
    // Format the output by adding 0 if it is single digit //
    if(s < 10){var s1='0' + s;}
    else{var s1=s;}
    if(m < 10){var m1='0' + m;}
    else{var m1=m;}
    if(h < 10){var h1='0' + h;}
    else{var h1=h;}
    
    // Display the output //
    var str = h1 + ':' + m1 +':' + s1 ;
    document.getElementById('n1').innerHTML=str;
    
    // Calculate the stopwatch //
    if(s < 59){ 
        s = s + 1;
    } else {
        s = 0;
        m = m + 1;
        if(m == 60){
            m = 0;
            h = h + 1;
        } // end if  m == 60
    }// end if else s < 59
    // end of calculation for next display
}
</script>

I want to change the button only for the clicked record row, not for the first record. Does anyone know how I can achieve this?

Answer №1

You are accessing the first instance of btn. Instead of using getElementById, try utilizing event.target. Here is an example:

<input 
  type="button" 
  name="btn" 
  id="btn" 
  value="Start" 
  data-ng-click="startTimeTracker(job)" 
  onclick="to_start(event)" 
/>

function to_start(event) {
  switch(event.target.value) {
    case 'Stop':
      window.clearInterval(tm); // stop the timer 
      event.target.value='Start';
      break;
    case 'Start':
      tm=window.setInterval('disp()',1000);
      event.target.value='Stop';
      break;
  }
}

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

Revamping this snippet - JavaScript with NodeJs

Currently working on a basic CRUD application, encountering an issue with obtaining the auto-incrementing value for the latest account in MongoDB. To provide more context, I've included the snippet below to achieve the following: 1) Conduct validati ...

How can AngularJS handle sorting based on the start date and end date?

Is there a way to filter the items by Start and End Date, based on the invoice_date, using the date range functionality in a meanjs app? I am facing an issue where the date filter functions work perfectly in Plunker and my localHost production environm ...

Creating a jQuery alertbox that is triggered by specific elements in an anchor tag

I am working on an HTML page that contains 50 A tags. I am trying to figure out how to create an alert based on a specific element inside the A tag. Here is an example of what I am looking for: <a href "something">click to see the alert</a> ...

Adjust the horizontal position of a text element using CSS

I am faced with a specific challenge where I need to center the text "test" within a text tag using only CSS, without being able to directly change the HTML. <text width="13.89" height="13.89" x="291.46999999999997" y="156.55" transform= ...

The offset values of $(element) keep increasing indefinitely when they are updated repeatedly

After researching how to utilize JavaScript drag functionality to move elements, the goal is to dynamically adjust the cursor position within a square when dragged. In an attempt to simplify the process and avoid storing x and y offsets as separate variabl ...

Unable to send an HTTP request using intent

I'm currently facing an issue where an intent triggers an HTTP request, but I keep encountering errors. Whenever the intent is triggered, the following code is executed: const https = require('https'); https.get('*************' ...

In search of a hover functionality similar to what can be found on Stack Overflow

I am really impressed by the hover effects on StackOverflow. I would love to incorporate a similar feature into my own web application. Can anyone provide me with more information? What is this feature called? Are there any libraries available for it? I h ...

SystemJS is loading classes that are extending others

In my Angular2 application, I have two classes where one extends the other. The first class is defined in the file course.ts (loaded as js) export class Course { id:string; } The second class is in schoolCourse.ts (also loaded as js) import {Cours ...

$PHP_SELF will be executed immediately upon the page loading

I recently designed an HTML page for inputting data into a MySQL database. In this setup, I've integrated PHP scripting directly within the HTML code. Take a look at the snippet below, <html> <head> </head> <body> <?php / ...

Aligning text in the center of a header, positioned beside an image

I am facing an issue with centering text within a banner. The banner is divided into 12 columns, with a cross icon placed in the left-most column for closing the window. However, the text is not centering within the whole banner width but only within the s ...

JavaScript loop used to create markers on Google Maps

I am currently in the process of developing a basic Google map which includes markers and involves looping through the data source for the markers. When I replace key.lng or key.lat with hardcoded values in the code below, everything functions correctly. ...

Injecting dynamic variables into JSON objects using JavaScript

I am facing a challenge with populating values dynamically from an array of elements. Below is the primary array that I am working with. list = [{name: 'm1'}, {name: 'm2'},{name: 'm3'},{name: 'm4'},{name: 'm5&ap ...

How to Embed Images from a Different Server Using PHPmailer

Currently, I am using phpmailer and AddEmbeddedImage to send emails. The web application I am working on sources data from two different servers: and . The website is hosted on and all scripts are executed from there as well. However, when I use phpmaile ...

Accessing Struts2 tag attributes with JavaScript

Currently, I am using struts2 with jsp. Within the JSP file, there is a table with several rows of data. To display the row data in the table, iterators are being used. Each row includes a button that allows the user to update the row's data, such as ...

Is it possible to find a match by searching through multiple arrays for a corresponding variable name?

Hi there, I'm currently working on a function that triggers an alert if the name of an array of images matches the data attribute of the selected element. This is just a test phase before proceeding with my main project, but I've hit a roadblock. ...

What is the syntax for invoking a function within a nested function in TypeScript?

Is there a way to call the function func2 from within the sample function of function func1? Any suggestions on how to achieve that? class A { public func1() { let sample = function() { //call func2... but ...

Make sure to properly check the size of the image before uploading it in express js

Below is the code I have written to verify if an image's size and width meet the specified criteria: im.identify(req.files.image,function (err,features) { //console.log(features); if(features.width<1000 ...

Maximizing the Potential of Return Values in JavaScript

I have a JavaScript code that retrieves information and displays it in a div. It's functioning properly, but I want to dynamically change the div id based on the returned data. For example: function autoSubmit3() { $.post( 'updatetyp ...

Tips for incorporating 'and' in the 'on' clause of 'join' in knex.js

I need assistance implementing the following SQL code in knex.js: select c.id,c.parent_id,c.comment,u.username,c.postid from comments as c join post_details as p on (p.id = c.postid and c.postid=15)join users as u on (u.id = c.userid); I attempt ...

Create a function to produce a list of dates within two specified date ranges using JavaScript

Looking for assistance as a beginner in JavaScript. Can anyone guide me on how to generate a list of dates between dateA and dateB? For instance: dateA = 07/01/2013 dateB = 07/01/2014 Desired outcome: 07/01/2013, 07/02/2013, 07/03/2013, 07/04/2013...a ...