16

I am working on a method of retrieving an array of hidden inputs in my form like so

<input type="hidden" value="12:34:00" name="timetemp0">
<input type="hidden" value="14:45:00" name="timetemp1">
<input type="hidden" value="15:12:00" name="timetemp2">
<input type="hidden" value="16:42:12" name="timetemp3">
<input type="hidden" value="16:54:56" name="timetemp4">
<input type="hidden" value="17:03:10" name="timetemp5">

My javascript function retrieves these individually by using getElementsByName('timetemp'+i)

    for (i ; i < counter[0].value; i++)
        {
//finds hidden element by using concatenation of base name plus counter

var timetemp = document.getElementsByName('timetemp'+i);

//if there is a value alert that value to user - this is just for testing purposes at the moment
//because there is only one of timetemp.i then it occupies position 0 in array
            if (timetemp[0].value == null)
            {
                alert ('No value');

            }
            else
            {
                alert (timetemp[0].value);

            }
}

So what should happen is it will alert the user of the value in that hidden input but if it comes accross an input with no value like this:

<input type="hidden" value="" name="timetemp16">

Then it will say "No value"

However th if function cannot seem to work with this:

I have tried:

  1. (timetemp[0].value == null)
  2. (timetemp[0].value === null)
  3. (timetemp[0].value == undefined)
  4. (timetemp[0].value == '')

It always seems to default to else clause.

Any ideas?

Kiquenet
  • 14,494
  • 35
  • 148
  • 243
sqlmole
  • 997
  • 7
  • 17
  • 31
  • Try just `if (timetime[0].value)` – Richard Dalton Aug 09 '11 at 13:55
  • 1
    http://stackoverflow.com/questions/154059/what-is-the-best-way-to-check-for-an-empty-string-in-javascript might help – Im0rtality Aug 09 '11 at 13:55
  • @Richard D you solution worked perfectly! I wish i could give you the points, could you answer my question maybe and i will accept your answer? – sqlmole Aug 09 '11 at 14:07
  • Very strange that your code didn't work when you did `.value == ''`. [Here's your code](http://jsfiddle.net/v8kC6/), only replacing the counter with a fixed number. It works fine. – user113716 Aug 09 '11 at 14:12

6 Answers6

42

Comment as an answer:

if (timetime[0].value)

This works because any variable in JS can be evaluated as a boolean, so this will generally catch things that are empty, null, or undefined.

Richard Dalton
  • 35,513
  • 6
  • 73
  • 91
6

In my opinion, using "if(value)" to judge a value whether is an empty value is not strict, because the result of "v?true:false" is false when the value of v is 0(0 is not an empty value). You can use this function:

const isEmptyValue = (value) => {
    if (value === '' || value === null || value === undefined) {
        return true
    } else {
        return false
    }
}
Jamter
  • 435
  • 5
  • 13
3

First, I would check what i gets initialized to, to see if the elements returned by getElementsByName are what you think they are. Maybe split the problem by trying it with a hard-coded name like timetemp0, without the concatenation. You can also run the code through a browser debugger (FireBug, Chrome Dev Tools, IE Dev Tools).

Also, for your if-condition, this should suffice:

if (!timetemp[0].value) {
    // The value is empty.
}
else {
    // The value is not empty.
}

The empty string in Javascript is a falsey value, so the logical negation of that will get you into the if-block.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91
1

Your script seems incorrect in several places.

Try this

var timetemp = document.getElementsByTagName('input');

for (var i = 0; i < timetemp.length; i++){
    if (timetemp[i].value == ""){
        alert ('No value');
    }
    else{
        alert (timetemp[i].value);
    }
}

Example: http://jsfiddle.net/jasongennaro/FSzT2/

Here's what I changed:

  1. started by getting all the inputs via TagName. This makes an array
  2. initialized i with a var and then looped through the timetemp array using the timetemp.length property.
  3. used timetemp[i] to reference each input in the for statement
Jason Gennaro
  • 34,535
  • 8
  • 65
  • 86
  • 2
    YOU ARE WRONG why would i want to find all inputs, I have over 50 inputs in my form , I only want to find timetemp0, timetemp1, timetemp2 - this does not adapt to this! – sqlmole Aug 09 '11 at 14:04
  • 1
    Fair enough @sqlmole. But no need to yell. I am only trying to help you. I answered based on your question, which does not mention other inputs. – Jason Gennaro Aug 09 '11 at 14:22
  • For other elements/attributes, the `.length` errors out on null. I ended up modifying it to be like this: `return (${jsElement} && ${jsElement}.length > 0)` – MarkHu Apr 25 '20 at 02:16
0

The counter in your for loop appears incorrect (or we're missing some code). Here's a working Fiddle.

This code:

//Where is counter[0] initialized?
for (i ; i < counter[0].value; i++)

Should be replaced with:

var timeTempCount = 5; //I'm not sure where you're getting this value so I set it.

for (var i = 0; i <= timeTempCount; i++)
James Hill
  • 60,353
  • 20
  • 145
  • 161
0

here's oneliner for checking for any values presented in array (null, undefined, '' in this case)

const isEmptyValue = val => [null, undefined, ''].includes(val);
Community
  • 1
  • 1