Replicating the existing div content into a textarea

I wanted to create a tool for my workplace as a learning project in html, CSS and JS. It's a timer that tracks how long I spend on tasks.

Currently, I have the timer resetting itself but I'm facing an issue where the paused time is not being logged to a text area when I reset the timer. Any assistance would be greatly appreciated!

Here is the current code I am working with:

var flagclock = 0;
var flagstop = 0;
var stoptime = 0;
var splitcounter = 0;
var currenttime;
var splitdate = '';
var output;
var clock;

function startstop() {
  var startstop = document.getElementById('startstopbutton');
  var startdate = new Date();
  var starttime = startdate.getTime();
  if (flagclock == 0) {
    startstop.value = 'Stop';
    flagclock = 1;
    counter(starttime);
  } else {
    startstop.value = 'Start';
    flagclock = 0;
    flagstop = 1;
    splitdate = '';
  }
}

function counter(starttime) {
  output = document.getElementById('output');
  clock = document.getElementById('clock');
  currenttime = new Date();
  var timediff = currenttime.getTime() - starttime;
  if (flagstop == 1) {
    timediff = timediff + stoptime
  }
  if (flagclock == 1) {
    clock.innerHTML = formattime(timediff, '');
    refresh = setTimeout('counter(' + starttime + ');', 10);
  } else {
    window.clearTimeout(refresh);
    stoptime = timediff;
  }
}

function formattime(rawtime, roundtype) {
  if (roundtype == 'round') {
    var ds = Math.round(rawtime / 100) + '';
  } else {
    var ds = Math.floor(rawtime / 100) + '';
  }
  var sec = Math.floor(rawtime / 1000);
  var min = Math.floor(rawtime / 60000);
  ds = ds.charAt(ds.length - 1);
  if (min >= 60) {
    startstop();
  }
  sec = sec - 60 * min + '';
  if (sec.charAt(sec.length - 2) != '') {
    sec = sec.charAt(sec.length - 2) + sec.charAt(sec.length - 1);
  } else {
    sec = 0 + sec.charAt(sec.length - 1);
  }
  min = min + '';
  if (min.charAt(min.length - 2) != '') {
    min = min.charAt(min.length - 2) + min.charAt(min.length - 1);
  } else {
    min = 0 + min.charAt(min.length - 1);
  }
  return min + ':' + sec + ':' + ds;
}

function resetclock() {
  flagstop = 0;
  stoptime = 0;
  splitdate = '';
  window.clearTimeout(refresh);

  if (flagclock == 1) {
    var resetdate = new Date();
    var resettime = resetdate.getTime();
    counter(resettime);
  } else {
    clock.innerHTML = "00:00:0";
  }
}

//Split function

function splittime() {
  if (flagclock == 1) {
    if (splitdate != '') {
      var splitold = splitdate.split(':');
      var splitnow = clock.innerHTML.split(':');
      var numbers = new Array();
      var i = 0
      for (i; i < splitold.length; i++) {
        numbers[i] = new Array();
        numbers[i][0] = splitold[i] * 1;
        numbers[i][1] = splitnow[i] * 1;
      }
      if (numbers[1][1] < numbers[1][0]) {
        numbers[1][1] += 60;
        numbers[0][1] -= 1;
      }
      if (numbers[2][1] < numbers[2][0]) {
        numbers[2][1] += 10;
        numbers[1][1] -= 1;
      }
    }
    splitdate = clock.innerHTML;
    output.innerHTML += (++splitcounter) + '. ' + clock.innerHTML + '\n';
    console.log("split");
  }
}

function time() {
  splittime();
  resetclock();
}
<input id="startstopbutton" class="buttonZ" style="width: 120px;" type="button" name="btn" id='btn' value="Start" onclick="startstop();">
<input id="resetbutton" class="buttonZ" style="width: 120px;" type="button" name="btnRst1" id='btnRst1' value="Reset" onclick="time();" />
<div id="clock" class="timerClock" value="00:00:00">00:00:00</div>
<br>
<textarea id="output" spellcheck="false" readonly></textarea>

Answer №1

Create a new function:

function logTime() {
    const time = document.getElementById('clock').getAttribute('value');
    document.getElementById('output').innerHTML += '\n' + time; 
}

Include it within the else block:

else {
    startstop.value = 'Start';
    flagclock = 0;
    flagstop = 1;
    splitdate = '';
    logTime(); // New functionality
  }

Ensure to update the element value, not just the innerHTML:

if (flagclock == 1) {
    clock.innerHTML = formattime(timediff, '');
    clock.setAttribute('value', formattime(timediff, '')); // Update attribute value
    refresh = setTimeout('counter(' + starttime + ');', 10);
  }

Testing phase:

var flagclock = 0;
var flagstop = 0;
var stoptime = 0;
var splitcounter = 0;
var currenttime;
var splitdate = '';
var output;
var clock;

// Existing functions here

function logTime() {
    const time = document.getElementById('clock').getAttribute('value');
    document.getElementById('output').innerHTML += '\n' + time; 
}
<input id="startstopbutton" class="buttonZ" style="width: 120px;" type="button" name="btn" id='btn' value="Start" onclick="startstop();">
<input id="resetbutton" class="buttonZ" style="width: 120px;" type="button" name="btnRst1" id='btnRst1' value="Reset" onclick="time();" />
<div id="clock" class="timerClock" value="00:00:00">00:00:00</div>
<br>
<textarea id="output" spellcheck="false" rows="4" cols="50" readonly></textarea>

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

I am curious as to why the Bootstrap 4 dropright selected menu appears as a default browser button in Chrome, while it functions seamlessly in Firefox

An issue has been identified that is specific to Chrome. The dropright selected menu appears differently in Chrome compared to Firefox, where it displays correctly with a white background. !https://i.sstatic.net/BulRA.jpg The Bootstrap version being used ...

Conflicting CSS selection elements in WordPress causing theme theme selection conflicts

Despite trying various edits to my custom CSS, such as including ::selection{color: white; background: #2f3f58;}, copying and pasting code from sites like W3Schools and stack overflow, nothing seemes to work. I am using the Hueman theme from , which has a ...

Is your CSS file functioning properly in Chrome but not in Internet Explorer?

I recently developed a custom SAP Portal Component that includes a JSP file with Javascript. This component is placed on a SAP Portal page alongside another iView, which contains multiple headers and text within an iframe. My objective is to dynamically ge ...

Create a sleek Bootstrap 4 webpage featuring a fixed footer and navigation bar. However, the challenge lies in ensuring that the content fills the

Currently, I am in the process of creating a template page that includes a navbar and a footer with the intention of adding additional columns within the main div (container-fluid) at a later stage. However, I have encountered two issues that I cannot seem ...

Exploring innovative methods for integrating dialog boxes in Chrome extensions?

Currently working on a Google Chrome extension and inquiring about the various choices for incorporating dialog boxes. I am looking for a solution that includes distinct head and body elements. The plan is to design custom-styled forms with jQuery. Are t ...

Is it recommended for the main, module, and browser properties in package.json to reference the minified version or the source

When developing a JavaScript library, it is important to consider the structure in which it will be published. This typically includes various bundles such as CJS, ESM, and UMD, each with their own source, minified, and map files. my-package\ dist& ...

Is it possible to independently verify the results of promises within a $.when(...) function without regard to the overall outcome?

Take a look at this example: $.when(a1, a2, a3, a4, .....) .done(function (){alert('done')}) .fail(function (){alert('fail')}); If all promises from a1 to ax were successful, I can manage and handle all the data within t ...

What is the most effective method for inputting a date/time into a Django view?

Looking to create a feature where users can see what events are happening at a specific time. What is the most efficient method to implement this request? For example, if I want to display all current events, should I submit a post request to /events/2009 ...

What is the best way to format specific text as bold within an input text field?

I am attempting to make certain text bold within an input text field. However, I'm uncertain about how to achieve this because HTML code is not recognized inside a text field. Therefore, using <b> will not be effective. Is there a way to bold sp ...

After the build process, Nextjs Sitemap is eliminating the /en/ from all newly generated web links

Utilizing Strapi to pull data in JSON format. For instance, a typical website link appears as follows: https:/ /www.some-site.com/some-link What happens to the links once the post build is completed on my Nextjs project: <url><loc>https://web ...

Changing the size of the Modal Panel in Ui Bootstrap for a customized look

Currently, I am in the process of developing an application that utilizes Ui bootstrap to seamlessly integrate various Bootstrap components with AngularJs. One of the key features I require is a modal panel, however, I found that the default size option &a ...

The React ternary operator within HTML does not display the correct HTML output

I'm currently learning React and facing a challenge with using a ternary operator. My goal is to display a minus sign by default, and then switch it to a plus sign when clicked. I implemented the ternary operator in my JSX and set the initial state of ...

Positioning Avatar component at the center of Card component

Hello there, I am currently trying to center my <Avatar> elements using Material UI with React within the <Card> components. The layout appears very crowded right now. What would be the most effective method to achieve this? I attempted setti ...

Failed Cross-Origin Request Sharing in AngularJS 1.4

I'm currently working with AngularJS version 1.4.3 and here is the code snippet I am using: angular .module('app', []) .run(run); function run($http) { a = $http({ method: "GET", url: 'http://127.0.0 ...

Lifting Formik's "dirty" value/state to the parent component: A step-by-step guide

Parent Component const Mother = () => { const [dusty, setDusty] = useState(false) return ( <ChildComponent setDusty={setDusty} /> ) } Child.js ... <Formik initialValues={initialValues} onSubmit={onSubmitHandler} validationSchema={sch ...

Is it better to parse HTML in PHP or JavaScript for an iPhone web application?

I am in the process of creating a web application for iPhone/iPod touch. I need to incorporate an external HTML file and extract specific data from it. One approach is to handle this on the server side by creating a separate page that echoes the desired d ...

Ways to assign the value of an alert to an element

Within this piece of code, my intention is to retrieve the alert value and apply it to an element. Description: The AJAX code I have written checks for values in a database, fetches new values from the database, and then displays these fetched values in a ...

Differences between Global and Local Variables in Middleware Development

While exploring ways to manage globally used data in my research, I stumbled upon this question: See 2. Answer After integrating the suggested approach into my codebase, I encountered a problem that I would like to discuss and seek help for. I created a ...

What is the best way to implement CSS Background-size: cover; while also making room for a menu?

Recently, I came across the CSS property background-size: cover, but I encountered a problem. I want the area behind my navigation on the left to be black. However, when I leave black space in the image itself, it gets cropped off when the image resizes. ...

Is there a way to horizontally align my navigation bar links with CSS while maintaining grey borders on the sides?

What is the best way to center my navigation bar links using CSS without losing the grey sides? Both Blogs and History have dropdown menus. Check out this screenshot: https://i.sstatic.net/2kvTm.png (source: gyazo.com) CSS: .navbar ul { list-styl ...