0

I want to save what the user types in the input as a variable when the user clicks the button. Than, I want to console.log the new variable with the users information. What am I doing wrong?

<html>    
    <div class="container bg-light">
        <div class="row">
    
          <div class="col-md-3 bg-danger">
          </div>
          <div class="col-md-6">
            <div class="form-group text-center">
              <h1>Saving User Data as a Variable with Javascript</h1>
    
              <div class="form-group">
                <input id="userdata" class="form-control">
              </div>
              <div class="form-group mx-auto text-center">
    
                <button type="button" onclick="saveUserData()" class="btn btn-danger btn-lg mx-auto w-50 text-center">Check console</button>
              </div>
            </div>
          </div>
          <div class="col-md-3 bg-danger">
          </div>
        </div>
      </div>
      
      <script>
      function saveUserData() 
      {
    	// store the tag with id="sign" in var userdata
    	var userdata = document.getElementById("userdata");
      }
      
      // confirm the element exists and what value the user submits
    	console.log(userdata);
    	console.log("users value is: " + userdata.value);
      </script>
</html>

1
  • You define userdata inside function saveUserData() but you try to show the value out of the function where userdata is not defined. Your script is also immediatly executed so you see an empty value Commented Apr 3, 2018 at 9:30

2 Answers 2

2

function saveUserData() {
   var name = document.getElementById('userdata').value;
   
   console.log("users value is: " + name);
}
<div class="container bg-light">
        <div class="row">
    
          <div class="col-md-3 bg-danger">
          </div>
          <div class="col-md-6">
            <div class="form-group text-center">
              <h1>Saving User Data as a Variable with Javascript</h1>
    
              <div class="form-group">
                <input id="userdata" class="form-control">
              </div>
              <div class="form-group mx-auto text-center">
    
                <button type="button" onclick="saveUserData()" class="btn btn-danger btn-lg mx-auto w-50 text-center">Check console</button>
              </div>
            </div>
          </div>
          <div class="col-md-3 bg-danger">
          </div>
        </div>
      </div>

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

Comments

0

try this

function saveUserData() {
 var userdata = document.getElementById('userdata').value;
 console.log("users value is: " + userdata);
}

3 Comments

didn't work I got this error... Uncaught ReferenceError: saveUserData is not defined at HTMLButtonElement.onclick
This code was ok. you can't defined saveUserData function at HTMLButtonElement.
@TonyCarbetta Edit your question to your latest code. And this answer needs some explanations.

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.