-2

I'm trying to make a string that if b (the password) = a (hello) it will alert "jj" and if not "nope" but I can't figure out why it won't work. I know this isn't how passwords are properly stored.

<!DOCTYPE html>
<html>
<body>

<input type="button" value="run js" onclick="start()">

<br><br>

UserName: <input type="text" id="userName">

Password: <input type="password" id="pasWord">


<p>hello</p>

<button onclick="passWord()"></button>


<script>
function start() {
    var a = ("hello");
}
function passWord() {
    var b = document.getElementById("pasWord").innerHTML;
    if (a = b) {
        alert("jj");
    } else {
        alert("nope");
    }
}

</script>

</body>
</html>
Cœur
  • 37,241
  • 25
  • 195
  • 267
greg-bt
  • 71
  • 7
  • Possible duplicate of [Which equals operator (== vs ===) should be used in JavaScript comparisons?](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) – Cœur Oct 18 '18 at 03:41

4 Answers4

1

This a = b isn't correct. It should change to the following:

a === b

Inside an if statement you should have only expressions that can be evaluated as booleans (true/false). Apparently, this a=b is an assignment of b to a and it isn't an expression that can be evaluated to true or false.

Furthermore, here b = document.getElementById("pasWord").innerHTML you should access the value attribute and not innerHTML, since this contains the value you are looking for.

Last but not least, you should see the declaration of the variable a. As it is, a is not accessible in passWord function.

You could try something like the following (despite it's a bit meaningless :))

var a;

function start() {
    a = "hello";
}

function passWord() {
    var b = document.getElementById("pasWord").value;
    if (a === b) {
        alert("jj");
    } else {
        alert("nope");
    }
}
<input type="button" value="run js" onclick="start()">
<br/>
UserName: <input type="text" id="userName">
Password: <input type="password" id="pasWord">
<p>hello</p>
<button onclick="passWord()">password</button>
Christos
  • 53,228
  • 8
  • 76
  • 108
1

using single "=" sign will assign the value to that variable. In your case use == or === to check if that String variable A is equal to "hello"

here is the solution :)

<script>
function start() {
    var a = ("hello");
}
function passWord() {
    var b = document.getElementById("pasWord").innerHTML;
    if (a === b) {
        alert("jj");
    } else {
        alert("nope");
    }

}
0

A few things are wrong here:

  • You access .innerHTML which is not the value of and input. To access the value use document.getElementById("pasWord").value.
  • You try to compare a and b with a = b which is an assignment, to compare use == or ===: a === b.
  • You declare a in the context of the start function, so its not accessible in the passWord function. You can either store it globally (just replace var a = ("hello"); with a = ("hello");, or better, access it directly in the passWord function.
Lux
  • 17,835
  • 5
  • 43
  • 73
0

you assigning b value to a. not comparing. To compare two value use === or ==.

Kalaivanan
  • 459
  • 2
  • 8
  • 17