1+ // This makes sure the document exists before your code tries to manipulate it.
2+ $ ( document ) . ready ( function ( ) {
3+
4+ // Attach an event handler function to the submit button.
5+ $ ( "#submit" ) . on ( "click" , function ( ) {
6+ // Store references to values of inputs
7+ var name = $ ( "#name" ) ;
8+ var email = $ ( "#email" ) ;
9+ var phone = $ ( "#phone" ) ;
10+ var message = $ ( "#message" ) ;
11+ // Store all values of required inputs into array
12+ var reqArray = [ name , email , phone ] ;
13+
14+ // Loop through all required elements
15+ for ( var i = 0 ; i < reqArray . length ; i ++ ) {
16+ // Check value of required item to ensure it is not empty
17+ if ( reqArray [ i ] . val ( ) === '' ) {
18+ // target message paragraph to display message user to double check their inputs
19+ message . text ( "Please fill out required fields." ) ;
20+ // add css class to message paragraph to alter its styling
21+ message . addClass ( 'warning' ) ;
22+ // target the associated label that exists adjacent to input, add class from stylesheet to alter its appearance
23+ reqArray [ i ] . prev ( "label" ) . addClass ( 'warning' ) ;
24+ } else {
25+ // if value is not empty, ensure the warning class is removed
26+ reqArray [ i ] . prev ( "label" ) . removeClass ( 'warning' ) ;
27+ }
28+ }
29+
30+ // if all labels on page do not have warning class, 'submit' form and notify user of success
31+ if ( ! $ ( "label" ) . hasClass ( "warning" ) ) {
32+ // alter the content of the header on page with custom string
33+ $ ( "#pre-form > h2" ) . html ( "Thanks for your feedback" ) ;
34+ // target the form and remove it from the DOM
35+ $ ( "#form" ) . remove ( ) ;
36+ }
37+
38+ } )
39+
40+ } ) ;
0 commit comments