0

Trying to get a variable from a HTML input field and pass it as parameter on a javascript constructor. I'm new to JS

Constructor

function Stopwatch(elem,count_Up,count_Down) {

  //Private Variables
  var time = 0; // Time in milliseconds
  var interval; // Used for update function
  var offset;   // 
  var countUp = count_Up
  var countDown = count_Down
  console.log('SWT'+countUp+count_Up)

Adding Values to constructor

function setCountTarget()
{
 var countTarget_Up = document.getElementById("count_Target").value;
 return countTarget_Up;

}

var watch = new Stopwatch(timer,setCountTarget(),setCountTarget()); 
//Create Stopwatch object 1
  • 2
    Do you have a question? – str Oct 09 '16 at 16:48
  • Do you have some reason to think that the code you have won't work? You need a clear problem statement and an [MCVE] (which would need to include the HTML you want to interact with!) – Quentin Oct 09 '16 at 16:48
  • the watch object is not taking in the countTarget_Up variable. – CollisionOfConcepts Oct 09 '16 at 16:52
  • I bet it's a duplicate of https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element but we cannot know without more code – Bergi Oct 09 '16 at 16:55

3 Answers3

0

You don't have to declare another variable inside the function.

function setCountTarget(){
   return document.getElementById("count_Target").value;
}

This returns a string type. If you want a number then you can do,

function setCountTarget(){
   return parseInt(document.getElementById("count_Target").value);
}

Well instead of an object constructor or instancing i would advise to do it in a loop. You get a data from user (probably end or start of the count) then you decrement from end or increment from start recursively. Think about your problem in every detail you might not need a constructor for this. It would be good to read about object oriented JS more.

Ali Somay
  • 585
  • 8
  • 20
0

Hard to say for sure, but I suspect that this is a timing issue - in order to get the value of an input node, that node needs to have been loaded into the DOM before the script is called. Try wrapping the Stopwatch() call in a window.onload function so that you can be sure than the DOM has been fully loaded:

function setCountTarget()
{
 var countTarget_Up = document.getElementById("count_Target").value;
 return countTarget_Up;
}

var watch ;//I'm assuming you want this to be a global variable;
window.onload = function(){    
   var watch = new Stopwatch(timer,setCountTarget(),setCountTarget()); 
}
Ben D
  • 14,321
  • 3
  • 45
  • 59
0

Could you be mixing up functions and values?

In Stopwatch

console.log('SWT'+countUp+count_Up)

should be

console.log('SWT'+countUp()+count_Up())

becasue you want to log the value of the function, not the code of the function.

And watch

var watch = new Stopwatch(timer,setCountTarget(),setCountTarget()); 

should be

var watch = new Stopwatch(timer,setCountTarget,setCountTarget); 

becasue you want to pass in the function, not the functions value.