I would like to check to see if a particular attribute of a DOM element is undefined - how do I do that?
I tried something like this:
if (marcamillion == undefined) {
console.log("Marcamillion is an undefined variable.");
}
ReferenceError: marcamillion is not defined
As you can see, the reference error is telling me that the variable is not defined, but my if
check is clearly not working, because it is producing the standard js ReferenceError
as opposed to the error message I am looking for in my console.log
.
Edit 1
Or better yet, if I am trying to determine if the attribute of an element is undefined like this:
$(this).attr('value')
What would be the best way to determine if that is undefined?
Answer
Using typeof
:
if (typeof marcamillion == 'undefined') {
console.log("Marcamillion is an undefined variable.");
}
Edit for the second question:
if ($(this).attr('value')) {
// code
}
else {
console.log('nope.')
}
No comments:
Post a Comment