31

Related to this question here. Can I check if an element in the DOM has a value or not? I'm trying to put it into an 'if' statement and not sure if my approach is correct. Here goes:

if (document.getElementById('customx')){
    //do something
}

Or should it be:

if (document.getElementById('customx') == ""){
    //do something
}

EDIT: By value I mean, customx is a text input box. How can I check if this field has no text entered.

3
  • Define "value". You mean text content? child elements? A value attribute? Commented Jan 10, 2012 at 12:42
  • What do you mean by value? <input type="text" value="myValue"> or <span>myValue</span>? Commented Jan 10, 2012 at 12:42
  • Hi, sorry I should have been more explicit in my question. I've updated my question Commented Jan 10, 2012 at 12:43

7 Answers 7

48

The getElementById method returns an Element object that you can use to interact with the element. If the element is not found, null is returned. In case of an input element, the value property of the object contains the string in the value attribute.

By using the fact that the && operator short circuits, and that both null and the empty string are considered "falsey" in a boolean context, we can combine the checks for element existence and presence of value data as follows:

var myInput = document.getElementById("customx");
if (myInput && myInput.value) {
  alert("My input has a value!");
}
Sign up to request clarification or add additional context in comments.

2 Comments

@simon Works fine on Chrome for me. Remember that you cannot access the elements of the document object until the DOMContentLoaded event has fired developer.mozilla.org/en-US/docs/Web/API/Window/…
thank you let me check out and I have problem I will going to tell you
9

getElementById will return false if the element was not found in the DOM.

var el = document.getElementById("customx");
if (el !== null && el.value === "")
{
  //The element was found and the value is empty.
}

Comments

2
var input = document.getElementById("customx");

if (input && input.value) {
    alert(1);
}
else {
    alert (0);
}

Comments

2

Empty is not the same as "no value".

HTMLInputElement.value is '' when you have an invalid entry. If .value is not '', then it's not empty. That's definite, but since there are times when .value === '' but not empty, we need to test. For example, in Chrome, with a number input type, if you type . or e it will be placed in the input box, but .value will not change from '' because it's invalid.

<input type=number>

We can ask the browser for more information and thankfully ValidityState is extremely well supported. Basically, if there's no value, but the browser says there's a bad input, then it's not empty.

function isInputEmpty(input) {
  return !input.value && !input.validity.badInput;
}

Edit: Credits to @osullic for find this in spec:

A control's value is its internal state. As such, it might not match the user's current input.

https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#a-form-control's-value

3 Comments

This is an interesting answer, but it would be good if you could link to some reference. I'd like to be sure that I can really rely across standard browsers that HTMLInputElement.value doesn't change from '' unless what's been entered passes validation.
@osullic Thanks for the reference. I discovered this because I'm trying to implement HTMLInputElement with Web Components and am trying to closely follow spec. There's a way to detect with :placeholder-shown, but you need a placeholder (even if blank) and then that can affect text selection. badInput seems more compatible.
0

Your first one was basically right. This, FYI, is bad. It does an equality check between a DOM node and a string:

if (document.getElementById('customx') == ""){

DOM nodes are actually their own type of JavaScript object. Thus this comparison would never work at all since it's doing an equality comparison on two distinctly different data types.

1 Comment

The first one is not correct either. It checks whether the element exists, not whether it has a value.
0

You want:

if (document.getElementById('customx').value === ""){
    //do something
}

The value property will give you a string value and you need to compare that against an empty string.

2 Comments

Keep in mind that " " is different to "" but hard to spot on screen.
Be aware that this crashes if the element does not exist.
0
var myInput = document.getElementById("customx");
if (myInput.value.length > 0) {
  //do something;
}

(.length > 0 is another way to check the input element)

1 Comment

Thanks for the answer. I added some context to make it clear what your solution brought to the table; in the future add some context for a higher quality answer. See how to answer for some tips.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.