Why is $ .isumeric false
I am writing JavaScript and I have a table where I need to check a field for a database key (number) or a default value ("NO MATCH"). I am trying to use jQuery 'isNumeric' function
Code:
var id = $('#myDataTable input:checked').map(function(){
return $(this).closest('tr').find('td:eq(3)').text();
}).get();
console.log(" ID "+id);
console.log(" ID2 "+$.isNumeric(id));
In my console, I see:
ID 1
ID2 false
Why is it wrong?
source to share
.get()
returns an array ["1"]
that is not numeric.
When you log it to the console, you cannot see it because you are concatenating it with a string, and ["1"].toString()
- "1"
.
Instead, when you are debugging this better, avoid forcing variables to other types. console.log("ID", id)
will be better.
To get a string instead of an array, you can use .get(0)
or [0]
.
However, if you are only interested in the first line, there is no point in using map
:
var id = $('#myDataTable input:checked').eq(0)
.closest('tr').find('td').eq(3)
.text();
source to share