0

I am facing the unexpected behavior of javascript function. Im an passing the ID of a field as string to function but it is receiving as bool value. Please help the code is below.

Function

function page_smooth_scroll(target_id) {
    if (target_id =! null) {
        $j('html, body').animate({
            scrollTop: $j("#" + target_id).offset().top - 120
        }, 500);
    }
}

calling function

function validatePassword(){
            var validPassword = false;
            var pwd = $j("#Password").val().trim();
            var cfmPwd = $j("#ConfirmPassword").val().trim();
            if((pwd == "") || (cfmPwd == "")){
                $j("#ConfirmPassword").addClass("invalidPwd").nextAll("ul.err-msg").html("<li>Please enter Password</li>");
              //here id is passed as string
                page_smooth_scroll("ConfirmPassword");
                validPassword = false;
            }
            else{
                $j("#ConfirmPassword").removeClass("invalidPwd").nextAll("ul.err-msg").empty();
                if(pwd != cfmPwd){
                    $j("#ConfirmPassword").addClass("invalidPwd").nextAll("ul.err-msg").html("<li>Password does not match</li>");
                  //here id is passed as string
                    page_smooth_scroll("ConfirmPassword");
                    validPassword = false;
                }
                else{
                    $j("#ConfirmPassword").removeClass("invalidPwd").nextAll("ul.err-msg").empty();
                    validPassword = true;
                }
            }
            return validPassword;
        }

the image is below while debugging passing string

receiving bool

1
  • You are using a =! operator... Should be != Commented Oct 25, 2016 at 6:20

1 Answer 1

5
if (target_id =! null) {

I think you mean a != b

Because a=!b means a = !b which means "assign the opposite boolean value", which will indeed turn anything into a boolean.

Next time if you think a function is "receiving a boolean", make sure to debug the value before running any statements. I'm sure it's still a string when going into the function.

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

1 Comment

if (target_id != null) can in most cases (when your ids cannot be empty strings, or 0, or false) also be written as if (target_id).

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.