Note that there are some explanatory texts on larger screens.

plurals
  1. PO.wrap(); breaking prevent.Default();
    primarykey
    data
    text
    <p>I've written a custom form validation script, but for some reason, wrapping <code>input[type=text]</code> elements in <code>&lt;div class="inputWrapper" /&gt;</code> stops me from preventing <code>input[type=submit]</code>'s default setting.</p> <p>Here's the relevant code:</p> <p><code>$("input[type=text]").wrap("&lt;div class=\"inputWrapper\" /&gt;");</code></p> <p>Is breaking:</p> <pre><code>$("input[type=submit]").click(function(event) { event.preventDefault(); }); </code></pre> <p>Why is this happening? If you need a more full script, let me know and I'll just post the whole thing.</p> <p>Update: Alright, so for some reason, disabling that line of code allows <code>.preventDefault</code> on the <code>input[type=submit]</code> to work, but if I just use</p> <pre><code>// wrap inputs $("input[type=text]").wrap("&lt;div class=\"inputWrapper\" /&gt;"); // validate on form submit $("input[type=submit]").click(function(event) { event.preventDefault(); }); </code></pre> <p>It works fine. So here's the full script, what could cause this weirdness?</p> <pre><code> $(document).ready(function() { // wrap inputs $("input[type=text]").wrap("&lt;div class=\"inputWrapper\" /&gt;"); $("textarea").wrap("&lt;div class=\"inputWrapper\" /&gt;"); // validate text inputs on un-focus $("input[type=text].required").blur(function() { if ($(this).hasClass("error")) { // do nothing } else if ($(this).val() === "") { $(this).addClass("error"); $(this).parent(".inputWrapper").append("&lt;div class=\"errorPopup\"&gt;" + $(this).attr("placeholder") + "&lt;/div&gt;"); } }); // validate textareas on un-focus $("textarea.required").blur(function() { if ($(this).hasClass("error")) { // do nothing } else if ($(this).val() === "") { $(this).addClass("error"); $(this).parent(".inputWrapper").append("&lt;div class=\"errorPopup\"&gt;" + $(this).attr("placeholder") + "&lt;/div&gt;"); } }); // validate on form submit $("input[type=submit]").click(function(event) { event.preventDefault(); // check fields $(this).parent("form").children("input.required").each(function() { // check textboxes if ($(this + "[type=text]")) { if (!$(this).val()) { $(this).addClass("error"); }; }; // end textboxes // check textareas $(this).parent("form").children("textarea.required").each(function() { if (!$(this).val()) { $(this).addClass("error"); }; }); // end textareas // check checkboxes and radio buttons if ($(this).is(":checkbox") || $(this).is(":radio")) { var inputName = $(this).attr("name"); if (!$("input[name=" + inputName + "]").is(":checked")) { var inputId = $(this).attr("id"); $("label[for=" + inputId + "]").addClass("error"); }; }; // end checkboxes and radio buttons }); // end fields // submit form var errorCheck = $(this).parent("form").children(".error").length &gt; 0; if (errorCheck == 0) { $(this).parent("form").submit(); } else { alert("You didn't fill out one or more fields. Please review the form."); window.location = "#top"; }; // end submit form }); // clear errors $("input.required").each(function() { // clear textboxes if ($(this + "[type=text]")) { $(this).keypress(function() { $(this).removeClass("error"); $(this).next(".errorPopup").remove(); }); }; // end textboxes // clear textareas $("textarea.required").each(function() { $(this).keypress(function() { $(this).removeClass("error"); $(this).next(".errorPopup").remove(); }); }); // end textareas // check checkboxes and radio buttons if ($(this).is(":checkbox") || $(this).is(":radio")) { var inputName = $(this).attr("name"); var labelFor = $(this).attr("id"); $(this).click(function() { $("input[name=" + inputName + "]").each(function() { var labelFor = $(this).attr("id"); $("label[for=" + labelFor + "]").removeClass("error"); }); }); }; // end checkboxes and radio buttons }); // end clear textbox errors }); </code></pre> <p>Update 2: Alright, I was wrong about what the problem was. It's related to the line I thought it was, but it's actually having an issue finding the <code>.error</code> after I wrap the inputs.</p> <p>Here's where the problem lies:</p> <p><code>var errorCheck = $(this).parent("form").children(".error").length &gt; 0;</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload