0

I'm trying to make a form that when you press the button it will validate, if the validation is successful if will call a function which will do a few things (saving form data to local storage etc).

The problem is I can't get the submitHandler to fire the function. Here is the jsFiddle I've been working on.

http://jsfiddle.net/JamesKrawczyk/xdo9nn48/

And the code.

<script>
function fireifworked()
{
$('.testIfWorking').html('IT WORKED');
}

$(document).ready(function() {
    $("#form1").validate({
        rules: {
            field1: "required"
        },
        messages: {
            field1: "Please specify your name"

        },
         submitHandler: function(form) {
            fireifworked();
        }
    })



    $('#btn').click(function() {
        $("#form1").valid();
    });
});
</script>

<form id="form1" name="form1"> 
    Field 1: <input name="field1" type="text" />
</form>

<div>
    <input id="btn" type="button" value="Validate"/>
</div>

<div class="testIfWorking">
CHANGE THIS IS WORKING
</div>
2

3 Answers 3

2

The button needs to be inside the form and be of type submit rathe than button

<script>
function fireifworked()
{
$('.testIfWorking').html('IT WORKED');
}

$(document).ready(function() {
    $("#form1").validate({
        rules: {
            field1: "required"
        },
        messages: {
            field1: "Please specify your name"

        },
         submitHandler: function(form) {
            fireifworked();
        }
    })



    $('#btn').click(function() {
        $("#form1").valid();
    });
});
</script>

<form id="form1" name="form1"> 
    Field 1: <input name="field1" type="text" />
     <input id="btn" type="submit" value="Validate"/>

</form>




<div class="testIfWorking">
CHANGE THIS IS WORKING
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

The . in the jquery selector is missing, it should be $('.testIfWorking').html('IT WORKED')

Comments

-1

Seems like jquery is not linked.

Add this in the header before your jquery code starts:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

There are several ways to start using jQuery on your web site. You can:

-Download the jQuery library from jQuery.com

-Include jQuery from a CDN, like Google as above example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.