Skip to content

Commit 204272d

Browse files
committed
ditch jQuery on the client side
1 parent 6495b05 commit 204272d

2 files changed

Lines changed: 59 additions & 66 deletions

File tree

assets/scripts/wplf-form.js

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,75 @@
22
* JS to be included on the front end pages with forms
33
*/
44

5-
(function($) {
6-
7-
$(document).ready(function() {
8-
9-
// ajax form submissions
10-
$('.libre-form').submit(function(e) {
11-
12-
var $form = $(this);
13-
14-
// add class to enable css changes to indicate ajax loading
15-
$form.addClass('sending');
16-
17-
// reset errors
18-
$form.find('.wplf-error').remove();
19-
20-
var data = new FormData($form[0]); // includes files too, but for some reason we don't see them at the server
21-
22-
23-
window.data = data;
24-
25-
26-
fetch(ajax_object.ajax_url + '?action=wplf_submit', {
27-
method: "POST",
28-
body: data
29-
}).then(function(response){
30-
//console.log(response);
31-
return response.text();
32-
}).then(function(response){
33-
console.log(response);
34-
response = JSON.parse(response);
35-
if( 'success' in response ) {
36-
// show success message if one exists
37-
$form.after(response.success);
38-
}
39-
if( 'ok' in response && response.ok ) {
40-
// submit succesful!
41-
$form.remove();
42-
}
43-
if( 'error' in response ) {
44-
// show error message in form
45-
$form.append('<p class="wplf-error error">' + response.error + '</p>');
46-
}
47-
$form.removeClass('sending');
48-
}).catch(function(error){
49-
console.log("Fetch error: ", error);
50-
$form.removeClass('sending');
51-
});
5+
(function(){
6+
document.addEventListener("DOMContentLoaded", function(){
7+
[].forEach.call(document.querySelectorAll(".libre-form"), function(form){
8+
9+
form.addEventListener("submit", function(e){
10+
11+
// add class to enable css changes to indicate ajax loading
12+
form.classList.add("sending");
13+
14+
[].forEach.call(form.querySelectorAll(".wplf-error"), function(error){
15+
// reset errors
16+
error.parentNode.removeChild(error);
17+
});
18+
19+
var data = new FormData(form);
20+
21+
window.data = data;
22+
23+
fetch(ajax_object.ajax_url + '?action=wplf_submit', {
24+
method: "POST",
25+
body: data
26+
}).then(function(response){
27+
28+
return response.text();
29+
30+
}).then(function(response){
31+
32+
response = JSON.parse(response);
5233

53-
// submit form to ajax handler in admin-ajax.php
54-
/*$.post( ajax_object.ajax_url + '?action=wplf_submit',
55-
$(this).serialize(),
56-
function(response) {
57-
console.log(response);
5834
if( 'success' in response ) {
5935
// show success message if one exists
60-
$form.after(response.success);
36+
37+
var success = document.createElement("p");
38+
success.className = "wplf-success";
39+
success.innerHTML = response.success;
40+
41+
form.parentNode.insertBefore(success, form.nextSibling);
42+
6143
}
44+
6245
if( 'ok' in response && response.ok ) {
6346
// submit succesful!
64-
$form.remove();
47+
form.parentNode.removeChild(form);
6548
}
49+
6650
if( 'error' in response ) {
6751
// show error message in form
68-
$form.append('<p class="wplf-error error">' + response.error + '</p>');
52+
53+
var error = document.createElement("p");
54+
error.className = "wplf-error error";
55+
error.textContent = response.error;
56+
57+
form.appendChild(error);
6958
}
70-
}
71-
).always(function() {
72-
// finished XHR request
73-
$form.removeClass('sending');
74-
});*/
7559

76-
// don't actually submit the form, causing a page reload
77-
e.preventDefault();
78-
return false;
60+
form.classList.remove('sending');
61+
62+
}).catch(function(error){
63+
64+
console.warn("Fetch error: ", error);
65+
form.classList.remove("sending");
66+
67+
});
68+
69+
// don't actually submit the form, causing a page reload
70+
71+
e.preventDefault();
72+
});
7973

8074
});
8175
});
82-
83-
})(jQuery);
76+
})();

classes/class-cpt-wplf-form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ function maybe_enqueue_frontend_script() {
453453
global $post;
454454

455455
// register the script, but only enqueue it if the current post contains a form in it
456-
wp_register_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array( 'jquery' ) );
456+
wp_register_script( 'wplf-form-js', plugins_url( 'assets/scripts/wplf-form.js', dirname(__FILE__) ), array() );
457457

458458
if( is_a( $post, 'WP_Post' ) && ( has_shortcode( $post->post_content, 'libre-form') || $post->post_type === 'wplf-form') ) {
459459
wp_enqueue_script( 'wplf-form-js' );

0 commit comments

Comments
 (0)