if(typeof (document.getElementById("courseId").value!=="undefined") || document.getElementById("courseId").value!==null)
{
Courseid = document.getElementById("courseId").value;
}
Asked
Active
Viewed 3.2k times
-2
-
1What is `#courseId`? An ``? – Eric Jun 20 '13 at 07:42
-
1http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-compared-to-undefined?rq=1 – Marvin Emil Brach Jun 20 '13 at 07:43
-
1OP is clearly a new user of SO and perhaps was not used to finding duplicate questions. It looks like a very reasonable question to me. Were all those downvotes actually necessary? – port5432 Oct 30 '15 at 10:32
-
I don't think this question is a duplicate since it's clearly asking about the type of a value in an input. – Shayan Mar 03 '19 at 15:30
3 Answers
9
rewrite it that way:
if(document.getElementById("courseId") && document.getElementById("courseId").value)
{
CourseId = document.getElementById("courseId").value;
}

Marco Forberg
- 2,634
- 5
- 22
- 33
2
If you explicitly want to check for undefined and null you can do
if(document.getElementById('courseId') === null ||
document.getElementById('courseId') === undefined) {
//logic
}

glosrob
- 6,631
- 4
- 43
- 73
0
An input's value will never be null
or undefined - it will be the empty string, ""
.

Eric
- 95,302
- 53
- 242
- 374
-
`document.getElementById("myInput").value.typeof` returns `undefined` for me. Even when I assign a value to it by typing in the input box like typing `20` it still returns `undefined`. – Shayan Mar 03 '19 at 15:22