What is wrong with my syntax in this conditional statement?

5472
14
Jump to solution
05-01-2015 11:13 AM
ChrisSergent
Regular Contributor III

I am trying to see if a field is empty, but I am receiving a syntax error. Any ideas:

on(dom.byId("btnFeedback"), "click", function () {

        If(document.getElementById("eMail").value == null || document.getElementById("eMail").value == ""); {

            alert("not working");

        } else {

         

        sendEmail();

       

    }

      

    );

Tags (2)
1 Solution

Accepted Solutions
ChrisSmith7
Frequent Contributor

Try this:

on(dom.byId("btnFeedback"), "click", function (e) {

    if (document.getElementById("eMail") == null || document.getElementById("eMail").value == "") {

             alert("not working");

    } else {

            sendEmail();

        }

});

Hope this helps! I confirmed this is working on my end...

The way it sits now, whenever someone clicks on "btnFeedback", it will throw an alert window if the "eMail" element isn't on the page, or, it is, but the value is empty, otherwise, we'll send an e-mail.

I made some minor mods, including lowering the case on your initial "If" to "if". I removed the "value" method on your first condition... it shouldn't ever be null, just "". I left the check because if it's not on the page, it will throw the alert. getElementById should always return an object, even if it's not found... it'll be a null object. Additionally, I removed the ";" from your conditional and fixed your mismatched brackets.

View solution in original post

14 Replies
SteveCole
Frequent Contributor

Semi-colon at the end of the if line?...

ChrisSergent
Regular Contributor III

Here is the complete block:

function sendEmail(ev) {

        on(dom.byId("btnFeedback"), "click", function () {

            If(document.getElementById("eMail").value == null || document.getElementById("eMail").value == "") {

                alert("not working");

            } else {

        

        sendEmail();

      

        }

     

    );

I removed the semi-colon but still receive a syntax error.

0 Kudos
DarrenWiens2
MVP Honored Contributor

sendEmail takes one parameter, but you pass none inside the else block. Although, you never actually use 'ev', so you may as well remove it.

DarrenWiens2
MVP Honored Contributor

You may get better results by flipping it around (I suspect the problem is to do with "== null"):

If((document.getElementById("eMail").value) && document.getElementById("eMail").value != "")

ChrisSergent
Regular Contributor III

Still getting a syntax error.

I have my code on github here if you want to see it: https://github.com/csergent45/streetSigns

I am working through two issues.

0 Kudos
SteveCole
Frequent Contributor

You said you have two issues. Is the other issue happening first? I've experienced situations where valid code broke due to an issue elsewhere in my code.

ChrisSergent
Regular Contributor III

I am just trying to figure out a toggle layer visibility. I re-uploaded my project as I comment out what I am not working on so they don't conflict.

0 Kudos
SteveCole
Frequent Contributor

How about....

If( (document.getElementById("eMail").value == null) || (document.getElementById("eMail").value == "") ) {

You could also try '===' (three equal signs) instead of '=='

ChrisSergent
Regular Contributor III

It's still an error with the extra )

0 Kudos