1

upon button click the id value should be validated and page should be loaded if the value is not NA or null otherwise it should throw an alert.

 <a href="#" id="check" class="button" onclick="id_check()">View ID</a><br><br>

function id_check() {
    if (id != "NA" || (id != null)) {
        // load diff page
    } else {
        alert("Something Wrong");

    }
}

But the if condition for or ( || ) is not working as expected. It worked fine when i just compared id != "NA". What am i missing here ?

7
  • Try !== and also you might want to include a !== undefined check as well. Commented Oct 17, 2015 at 6:14
  • You can also check if(id) { .. } else {... } Commented Oct 17, 2015 at 6:15
  • @potatopeelings: comparing id != null already hits undefined... Commented Oct 17, 2015 at 6:29
  • @dandavis - you are right. But my suggestion was to change it to !== after which you'll need to check for undefined separately. Cheers! Commented Oct 17, 2015 at 7:51
  • 1
    @potatopeelings: but why would you turn one thing that does both the things you want into two things that still do both the things you want? more to type and more work to evaluate... Commented Oct 17, 2015 at 10:22

3 Answers 3

4

The important thing to understand here is || operator short circuits the result. It means that, if the first expression evaluates to be truthy then that value will be returned immediately. The second expression will not be evaluated at all.

In your case, id != "NA" || id != null, if the data is null then id != "NA" check will return true. So, the second part is not checked at all.

As you want the input data not be NA and null, you can use && operator here, like this id != "NA" && id != null

Note: if != null will return false, if id is undefined, because != will coerce the expressions. So, it is better to use strict equality comparison operators. So, the condition would become if id !== "NA" && id !== null

Sign up to request clarification or add additional context in comments.

Comments

3

Your condition should be id != "NA" && id != null as you want id to be distint from NA and distinct from null too.

Comments

0

Please use following code which will display 'successfully' text as a message if id is not empty.

<a href="#" id="check" class="button" onclick="id_check(this.id)">View ID</a>

    <script>
     function id_check (id)
                 {
                //if block will execute if element id value isn't empty or Null
                     if ( id )
                     {

                        alert("Id isn't empty");
                     }else
                     {
                         alert ("Id is empty");

                     }
                 }
    </script>

We need to pass id of control to function.After pass id we can verify it according to our requirement.

this.id - it will return id of element as a parameter to the 'id_check' function.

Refer following link to verify answer - http://jsfiddle.net/pdhsob8s/4/

Comments

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.