JQuery form submit not rendered if if statement

I have this error, but I can't see how to fix it. I have used JSLint with some results (it just tells me it is document

not a global value, or an "else clause" is not needed, etc.) but has no solution.

I have a form to submit and I need to compare that both email fields are equal. So I want to have a simple simple email1 == email2 script, jQuery is not my strongest point, but I came up with the following:

My form id is "regIn", my email fields are type="email"

(HTML5) and id="MailA"

and id="MailB"

respectively.

I preloaded jquery 1.11.1.min.js

My jQuery code (see below why #forgotten

is still there):

$(document).ready(function() {

    $('#forgotten').click(function() {
        $('#passbox').toggle(360);
        return false;
    });

    $('#regIn').submit(function() {

      

// * link; but this is not in the code:alert('alert1');

        if ($('#MailA').val() === $('#MailB').val()) {
            return true;
        }
        $('#errorMsg').innerHTML("Emails need to be the same!");
        alert('Hello');
        return false;
    });
});

      

So - I use Alerts to see how far the script goes (I know, sticky debugging). firebug does not report page errors or script errors that I see. "Warning 1" always fires on form submission and successfully shows me the values #MailA.val()

and #MailB.val()

,

But the operator if

doesn't seem to fire at all. I tried to make variable values ​​( var d1 = $('#MailA').val()

etc.), but that doesn't change anything, the form is always submitted, and neither if

nor else

(which surrounded the text under the operator clause if

but JSLint said it was not needed, so I removed it).

I also used the syntax variations, using !=

and ==

, but also read out JSLint, that ===

and !==

are preferred.

My HTML:

 <form enctype="multipart/form-data" name="regIn" id="regIn" autocomplete="off" accept-charset="utf-8" action="#" method="post">

    <div class="inputContainer">
        <input type="email" value="" tabindex="31" id="MailA" name="regMailA" required>
    </div>

    <div id="errorMsg"></div>

    <div class="inputContainer">
        <input type="email" value="" id="MailB" tabindex="32" name="regMailB" required>
    </div>

    <div class="registerRow">
        <input type="submit" class="regButton" value="Register" tabindex="34">
    </div>
</form>

      

As I said, I've looked around in the last hour or so and I just don't understand what it is.

Obviously the code is stripped away for the question, the only other aspect is that document ready

there is another piece of jQuery in the function - as shown in the picture #forgotten

to show / hide information. box, it works great.

+3


source to share


1 answer


note that you are using:

$('#errorMsg').innerHTML("Emails need to be the same!");

      

if you try to notify $('#errorMsg').innerHTML

it will be undefined.



When using JQuery selectors, JQuery $('#errorMsg')

will wrap the dom object with a JQuery dom object which has special JQuery methods (val, html, etc.), so you don't have access to the main method innerHTML

, instead you have to use:

$('#errorMsg').html("Emails need to be the same!");

      

Enjoy!

+1


source







All Articles