0

I am attempting to simply pull an input from an html form and manipulate it with JavaScript. When I console.log the variable I created in JavaScript I am just getting my html element back. If I attempt to add [0] or .value, I get undefined and a JQuery error, respectively. I am at a loss as to the reason this isn't working. If it matters I am doing all this in CodePen.

HTML:

<div>
  <form id="myForm">
  Construction Year: <br>
  <input type="text" id="construction_field" name="construction_field" value=""><br>
  </form>
  <button onclick="myFunction">Find P</button>
  <p id="demo"></p>
</div>

JavaScript:

$(document).ready(function(){ 
  function myFunction () {
  var constructionYear = document.getElementById("construction_field");
  console.log(constructionYear);
  }
});

I apologize for such a simple question, but I can't find an answer that works to save my life. If it isn't obvious I am very new and struggling, any examples would be amazing.

2

2 Answers 2

1

If you wish to use jQuery:

var constructionYear = jQuery('#construction_field').val();

If you wish to use vanilla JavaScript:

var constructionYear = document.getElementById('construction_field').value;
Sign up to request clarification or add additional context in comments.

1 Comment

I think you were right about value. After reworking the code you answer worked. Thanks for the help!
0

Just use console.log(constructionYear.value);

2 Comments

Thanks so much! I'm pretty sure the value was the issue. I accidentally deleted some code and when I rewrote it with value it worked!
If the answer was useful to you, please upvote/accept. Thanks.

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.