-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_values2.html
84 lines (64 loc) · 2.48 KB
/
form_values2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Form Values</title>
<meta name="viewport" content="user-scalable=0, initial-scale=1.0">
<link rel="stylesheet" href="styles/normalize.css">
<link rel="stylesheet" href="styles/base.css">
</head>
<body>
<!-- NOTE: .js files are at bottom of page -->
<div id="wrapper">
<h1>jQuery Form Values:<br>
Capturing Names and Values</h1>
<form id="picker">
<label for="first">Value 1: </label>
<input type="text" id="first" name="first" class="textfield"><br>
<label for="second">Value 2: </label>
<input type="text" id="second" name="second" class="textfield"><br>
<label for="third">Value 3: </label>
<input type="text" id="third" name="third" class="textfield"><br>
<label for="fourth">Value 4: </label>
<input type="text" id="fourth" name="fourth" class="textfield"><br>
<input type="submit" value="Save">
<input type="reset" value="Reset">
</form>
<!-- emepty paragraph we will write into -->
<p id="result"></p>
<p>How does this work? See notes in the JavaScript at the bottom of this
HTML file (view source).</p>
<p>Like this? Thank <a href="https://twitter.com/macloo">@macloo</a></p>
<p><a href="https://github.com/macloo/jQuery_useful">View on GitHub.</a></p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"> </script>
<script>
// test to ensure jQuery has loaded
$('#wrapper').append('<p>Hello!</p>');
// get name and value of all text input fields
// run this when submit button is clicked
$('#picker').submit(function(e) {
// look at each child of the form that has the ID picker
$(this).children().each(function() {
// below, (this) is the individual form element
// do only if the form element is a text field
if($(this).attr('type') == 'text') {
// get the name of the element
$('#result').append( "name: " + $(this).attr('name') );
// get the value of the element
$('#result').append( ", value: " + $(this).val() + '<br>');
}
});
e.preventDefault(); // form is not submitted
});
</script>
<!-- Sources:
https://api.jquery.com/submit/
http://stackoverflow.com/questions/8088343/using-jquery-each-and-children-to-traverse-and-hide-focus-form-elements
How to structure an HTML form
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/How_to_structure_an_HTML_form
My first HTML form
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form
-->
</body>
</html>