Javascript | Adding value to String Returns an Undefined

80 Views Asked by At

I am new to Javascript so please forgive my ignorance.

I got a dynamic table that increases with one row each time the user enters a value.

I'm trying to get all values from the new created column to appear in a TD (id=scm) seperated with a dot.

The following works but returns an "UNDEFINED" at the start.

  var serial = document.getElementById("serial");
  var scm = document.getElementById("scm");
  if (scm === ""){
  scm.innerHTML = "start";}
  else {
  scm.innerHTML = scm.value += "." + serial.value }

All help is welcome!

1

There are 1 best solutions below

3
On

I think at the start, the table might not be initialised so you're correct checking for scm === "" but you also need to check for undefined (since the table is empty when it loads, right?) so the logic will flow to the else clause and scm will be undefined!

Try adding something like this:

if (!scm || scm === ""){

This will check if scm has no value i.e. undefined, OR if it's an empty string. :)