# Setting Javascript Variable Equal To User Input

**URL:** <https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 17, 2018, 9:17am UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089 "2018-02-17T09:17:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alioune01](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alioune01/32/195636_2.png) [@Alioune01](https://forum.freecodecamp.org/u/Alioune01)\
**Post date:** [February 17, 2018, 9:17am UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089/1 "2018-02-17T09:17:23Z")

</div>

The following javascript code is supposed to compare two values entered into two textfields to see if they’re equal. Here is that code:

```auto
var password = document.getElementById("password").value;
var confirmPassword = document.getElementById("confirmPassword").value;
var errorMessage = document.getElementById("errorMessage");
var createAccountButton = document.getElementById("createAccount");
var chosenPassord;

createAccountButton.addEventListener("click", function()
                                              {
												  if(confirmPassword === password)
												  {
													  chosenPassword = password;
													  errorMessage.innerHTML = "";
													  console.log("confirmPassword is " + confirmPassword + ",password is " + password);
												  }
												  
												  else if(confirmPassword != password)
												  {
													  errorMessage.innerHTML = "Passwords don't match!";
													  console.log("bad");
												  }
                                              });

```

That code is in a file called CreateAccount.js and is used with the following html:

```auto
<!Doctype html>
<html>
	<head>
		<link href = "SampleProjectCreateAccountPage.css" rel = "stylesheet" type = "text/css"/>
	</head>
	
	<body>
		<div id = "container">
			<div id = "loginContainer">
				<label>Username</label><input id = "username" type = "text" class = "customizeInput"></input> <br/> <br/>
				
				<label>Email</label><input id = "email" type = "text" class = "customizeInput"></input> <br/> <br/>
				
				<label>Password</label><input id = "password" type = "password" class = "customizeInput"></input> <br/> <br/>
				
				<label>Confirm Password</label><input id = "confirmPassword" type = "password" class = "customizeInput"></input> <br/> <br/>
				
				<p id = "errorMessage"></p>
				
				<button id = "createAccount" class = "customizeButton" >Create Account</button> <br/> <br/>
				
				<button id = "cancel" class = "customizeButton" onclick = "location.href = 'SampleProjectLoginPage.html'">Cancel</button>
			</div>
		</div>
		<script src = "CreateAccount.js">
	    </script>
	</body>
</html>

```

My problem is that it always says that password and confirmPassword are equal no matter what you enter. I notice that if you manually set the values to different things in the html it will react to _that_, which may be part of the problem. However, when I see examples such as [this one](https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_password_value), it seems as though what I have done should work.

What is going wrong?

---

<div class="post-metadata">

**Author:** ![PortableStick](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/portablestick/32/65591_2.png) [@PortableStick](https://forum.freecodecamp.org/u/PortableStick)\
**Post date:** [February 17, 2018, 4:24pm UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089/2 "2018-02-17T16:24:46Z")

</div>

`password` and `confirmPassword` are set only once, when the page is first loaded. As such, they’re always going to be empty strings. You need to move those variables inside your click handler in order to get the current values.

---

<div class="post-metadata">

**Author:** ![Alioune01](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/alioune01/32/195636_2.png) [@Alioune01](https://forum.freecodecamp.org/u/Alioune01)\
**Post date:** [February 19, 2018, 6:24pm UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089/3 "2018-02-19T18:24:31Z")

</div>

Do you have any suggestions on how I would do this?

---

<div class="post-metadata">

**Author:** ![camperextraordinaire](https://avatars.discourse-cdn.com/v4/letter/c/5f9b8f/32.png) [@camperextraordinaire](https://forum.freecodecamp.org/u/camperextraordinaire)\
**Post date:** [February 19, 2018, 6:47pm UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089/4 "2018-02-19T18:47:45Z")

</div>

@PortableStick told you where to move your code. Move all of the code at the top to be inside click event’s callback function.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:59am UTC](https://forum.freecodecamp.org/t/setting-javascript-variable-equal-to-user-input/175089/5 "2021-02-02T11:59:50Z")

</div>

Closed on February 2, 2021, 11:59am UTC


