Anton van Elburg
Anton van Elburg

Reputation: 13

string length condition not working for webhook

I'm still new to JavaScript but I wrote a small script that sends messages to a webhook. Which works great, but now I need to set some conditions to it before it may execute. The condition I wanted to set here was that the field "name" has to have a minimal length of 9 characters. I just can't find the fault in this if statement.

function json()
{
  if(document.getElementById('name').length >= 9)
  { 
    var msgJson = {
      "text": "success",
    };
  send(msgJson);
  }
}

Does anyone have the answer for me?
Thanks in advance!

Upvotes: 1

Views: 579

Answers (2)

Martin Adámek
Martin Adámek

Reputation: 18389

document.getElementById('name') returns HTML element reference. If you want to check the length of that input value (<input id="name">), then you need to use document.getElementById('name').value and check the length of it, like this:

function json() {
    var el = document.getElementById('name');
    if (el.value.length >= 9) {
        var msgJson = {
            "text": "succes",
        };
        send(msgJson);
    }
}

Upvotes: 2

Derek Brown
Derek Brown

Reputation: 4419

The getElementById() function returns an Element object (source). This has no .length property- so you are going to get an error.

Upvotes: 0

Related Questions