Reputation: 1239
I have a codebit where I'm making a form. There is one numerical input, and I want to know how to get that input and store it in a variable for JS calculation. How can I take the current value in the input on submit and make it a variable?
$(Document).ready(function() {
$("input[type='submit']").click(function() {
});
});
body {
background-color: skyblue;
}
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='style.css' />
<script src='script.js'></script>
<meta charset="UTF-8">
<title>Classified</title>
</head>
<body>
<h1>Classified</h1>
<form action="script.js" name="classified" method="get">
<input type="number" name="classified" min="0">
<input type="submit" value="submit">
</form>
<h3 hidden>Classified</h3>
<h3 hidden>Classified</h3>
</body>
</html>
Upvotes: 0
Views: 51
Reputation: 4584
If you are allowed to change the markup of the form I would suggest giving it an ID e.g.
<form action="script.js" name="classified" method="get">
<input id="my-id" type="number" name="classified" min="0">
<input type="submit" value="submit">
</form>
Then in your JS
$(document).ready(function() {
$("input[type='submit']").click(function() {
var myNumVal = $('#my-id').val();
});
});
jQueries .val()
methods return the value
property of form elements.
Also, as stated in the comments below your post - I also believe Document
is case-sensitive and should be document
to atleast be safe that your code is actually running.
Upvotes: 1
Reputation: 24965
$(function() {
//user could hit enter in the input field, bind on submit instead
$("form[name='classified']").on('submit', function() {
var $classified = $('input[name="classified"]');
console.log($classified.val());
});
});
Upvotes: 2