Include a text input and a span element inside an ASP.NET ListView

I am working on an asp.net listview that includes a column with 2 input buttons (id=Start, id=Stop), 1 text input (id=TimeMinutes), and 1 span (id=spCountDown). The generated HTML for these controls within the listview looks like this...

<table id="ctl00_ContentPlaceHolder1_lstViewFormulas_itemPlaceholderContainer" style="vertical-align:top; border:solid 1px gray">
        <tr id="ctl00_ContentPlaceHolder1_lstViewFormulas_Tr1" style="vertical-align:top; border:solid 1px gray">
            <td class="ListViewHeader" style="width:20%">
                 <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_lblFormName">Step Name</span></td>
            <td class="ListViewHeader" style="width:10%" align="center">
                 <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_lblTiming">Timing</span></td>
            <td class="ListViewHeader" style="width:30%">
                 <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_Label6">Area Used</span></td>
            <td class="ListViewHeader" style="width:10%">
                 <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_lblClock">Timer</span></td>
            <td class="ListViewHeader" style="width:30%">
                 <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_lblProd">Products</span></td>
        </tr>


        <tr style="">
           <td>
              <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl0_lblFormName">Step 1</span>
           </td>
           <td>
              <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl0_lblTiming">20</span>
           </td>
           <td>
              <span id="ctl00_ContentPlaceHolder1_lstViewFormulas_ctrl0_lblAreaUsed">Scalp</span>
           </td>
           <td>

            <input type="button" onclick="countdown(document.getElementById('TimeMinutes'), document.getElementById('spCountDown'))"; value="Start" id="Start" />
            <input type="button" onclick='stopcountdown()'; value="Stop" id="Stop" />


            <input type="text" value='20' id="TimeMinutes" />
              <span id="spCountDown"></span>
           </td>

My goal is to pass the values from the "TimeMinutes" text input and the "spCountDown" span into a JavaScript function.

I have tried various methods, including...

<input type="button" onclick="countdown(document.getElementById('TimeMinutes'), document.getElementById('spCountDown'))"; value="Start" id="Start" />

Additionally, I attempted to locate these controls in the JavaScript function using their IDs, but haven't been successful in accessing the correct row.

The objective is to extract the value from the text input and initiate a countdown timer within the JavaScript function.

  var interval;
  var seconds=0;
  function countdown(txtminutes, spanid) {
      interval = setInterval(function () {
          var sp = document.getElementById("spanid");
          var minutes = document.getElementById("txtminutes").value
          if (seconds == 0) {
              if (minutes == 0) {
                  sp.innerHTML = "countdown's over!";
                  clearInterval(interval);
                  return;
              } else {
                  minutes--;
                  seconds = 60;
              }
          }
          if (minutes > 0) {
              var minute_text = minutes + (minutes > 1 ? ' minutes' : ' minute');
          } else {
              var minute_text = '';
          }
          var second_text = seconds > 1 ? 'seconds' : 'second';
          sp.innerHTML = minute_text + ' ' + seconds + ' ' + second_text + ' remaining';
          seconds--;
      }, 1000);
  }

  function stopcountdown() {
      window.clearInterval(interval);
      var sp = document.getElementById("CountDown");
      sp.innerHTML = ''
  }

Can anyone guide me on how to effectively pass these controls in order to manipulate the correct values in the right row?

Appreciate any assistance!

Answer №1

Make sure you're only calling getElementById once!

Instead of:

<input type="button" onclick="countdown(document.getElementById('TimeMinutes'), document.getElementById('spCountDown'))"; value="Start" id="Start" />

Use:

var minutes = txtminutes.value;

Don't pass a reference as a parameter and then try to create a new reference with the same id. Simply eliminate the document.getElementById part.

And remember to convert the string to an integer using parseInt()

Answer №2

Apologies for the hasty response earlier. It appears that using parseInt() to convert the value from the input element (which is a string) to an integer would be beneficial in this scenario.

var minutes = parseInt(document.getElementById("txtminutes").value)

In the If statement below, there seems to be a comparison between a string and an int, unless I am mistaken.

Furthermore, it might be helpful to store the timer value in a variable rather than converting the string from the input-element every second. Consider this approach: refrain from extracting the time from the input element; instead, utilize it solely for displaying the current time. This method is not only safer but also potentially less resource-intensive.

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

Encountered a TypeScript issue when using React and Slate: The property 'renderElement' is not found on type 'IntrinsicAttributes'

Currently, I am working on mastering Slate by following the steps outlined in this tutorial: Defining Custom Elements. After adapting the code slightly from the tutorial, here is what I have integrated: export default function App() { const renderEleme ...

Utilizing Radio buttons to establish default values - a step-by-step guide

I am utilizing a Map to store the current state of my component. This component consists of three groups, each containing radio buttons. To initialize default values, I have created an array: const defaultOptions = [ { label: "Mark", value: & ...

Running the test suite in another tab is causing it to fail

I am experiencing an unusual issue in my protractor UI test. During one of the tests, I need to click on a link that opens in a new tab. The test passes when run individually, but fails when run as part of the test suite. I would appreciate it if you coul ...

Having trouble formatting JSON data in a jQuery datatable with accurate information

Currently, I am diving into the world of jQuery tables specifically for a report that I am working on. Despite successfully receiving the API response, I am facing challenges in binding it to the jQuery datatable. I have searched through various questions ...

What is the name of the undefined JSON object and how can I retrieve its parameters?

After sending a request to a web server, I received the following response: The response, when using JSON.stringify, appears as shown below: undefined{\"access_token\":\"Rhazjww5 ...QUiTMVc\",\"token_type\":\"bearer&bs ...

Leverage a custom server (such as NestJS) within NextJS to dynamically render targeted pages

I am experimenting with using NestJS as a custom server for NextJS, following the instructions in this article. Here is a simplified version of the code: @Controller('/') export class ViewController { @Get('*') async static(@Req() r ...

I am unable to access the information from a .txt file using Ajax

I'm attempting to create a basic AJAX example, but I'm encountering issues getting it to function correctly. Below is the code I have: <script> function loadXMLDoc() { var xmlhttp; if (window.XMLhttpRequest) { ...

Avoiding the parsing of JSON strings in JavaScript

var data = Sheet : [ {"Cell_Address":"A1","Cell_Type":"1","Cell_Value":"Name"}, {"Cell_Address":"B1","Cell_Type":"1","Cell_Value":"Phone no."}, {"Cell_Address":"C1","Cell_Type":"1","Cell_Value":"Currency"}, {"Cell_Address":"D1","Cell_Type":"1","Cell_Value ...

Unable to use npm module "csv-db" as it is currently experiencing functionality issues

Looking to implement a "lightweight offline database" that stores data in .csv files. The module I am using can be found in the documentation: https://www.npmjs.com/package/csv-db Unfortunately, I've been encountering issues with this module and have ...

Implementing event listener for component generated by mapping function

I am using the map function in Javascript to render a list, but I am struggling to add a click listener to the elements in that list. Check out the code here - https://codesandbox.io/s/oqvvr1n3vq My goal is to log Hello to the console whenever the h1 tag ...

Ensure that the csrf token is included on all routes

I have successfully implemented CSRF protection in my Node.js web application, but I am facing an issue where the CSRF token is only attached if I visit the /login route. How can I ensure that the CSRF token gets implemented on every route without causing ...

Error: The spread operator cannot iterate over a null value

I am struggling to resolve the issue with a null spread operator that occurs when my charities (deconstructed from props) is empty. How can I handle this scenario effectively? Any suggestions or guidance on how to tackle this would be much appreciated. The ...

What is the best way to make AngularJS acknowledge invalid input that has already been identified by the browser?

Encountering an issue (specifically in Chrome) where a number input is deemed invalid by the browser, but valid as per Angular. <form name="form_name"> <input type="number" step="any" id="a_number" name="a_number" min="0" ng:model="aN ...

Error encountered when attempting to retrieve HTML content from localhost using AJAX

Attempting to insert HTML code into a div element using ajax, similar to how it's done with an iframe. Testing this out locally first to avoid Same Origin Policy complications. The web application is currently hosted on a wamp server. There are two ...

stay at the top of the screen with anchor #link

Is there a way to link to a page with a specific bootstrap nav-tabs open without the page automatically scrolling down to that tab? I've tried using #link with the tab id, like www.mysite.com/apagewithtabs#tab2, but I want the user to be at the top of ...

To enable the addition of a value to the database in VB.net when no selection is made from the dropdown,

I am facing an issue with my dropdown list that is supposed to add new titles to my database. I want to include a textbox alongside the dropdown to allow users to manually type in a new title if none of the options in the dropdown are suitable for their ne ...

Create a dynamic dropbox using JavaScript/JQuery in an HTML page

I am currently working on implementing 3 different drop down boxes that are triggered by 4 different buttons. These drop down boxes should appear within the #apple div. When Button B1 is clicked, the drop down options include Apple, Mango, and Papaya. Whe ...

What is the best way to add up values from text fields with the same ID?

I am working with input fields that are set up like this <input id="nilai" name="nilai" type="text" value="10" readonly /> <input id="nilai" name="nilai" type="text" value="10" readonly/> <input id="nilai" name="nilai" type="text" value="10 ...

Adjusting the speed of Flexslider with mousewheel control

I am looking to implement flexslider with mousewheel functionality. Here is an example of how I want it to work: $('#slider').flexslider({ animation: "slide", mousewheel: true, direction: "vertical", ...

Show only the objects in a MongoDB collection that have a matching status in another collection

I have two different collections - one called competition and the other called product. The competition collection contains the objectID of the product, while the product collection contains the competition_status. My goal is to only display the competiti ...