0

I have a very small jQuery script that I would like to add to a single page. I am trying to add it directly in the page via the page editor in the Wordpress backend. Since it is so small and does not apply to any other page on my site, I would prefer to add it to the page directly. So, in the page editor I click on the 'Text' tab and include the following:

<script type="text/javascript">
$("form").submit(function( event ) {
    if($('#somecheckbox').is(':checked')) {  
        alert( "Handler for .submit() called." );
    }  
    else {  
        event.preventDefault();
    }
});
</script>


<form action="/helpdesk/" method="post">
    <input id="somecheckbox" type="checkbox" value="yes" /> <label for="somecheckbox">Checkbox</label>
    <input type="submit" value="Submit" />
</form>

This doesn't work though and so I am wondering if it's even possible. Problem is that the alert box doesn't pop up and the submit doesn't get cancelled either.

I know how to include the script as a .js file, just hoped I didn't have to.

0

2 Answers 2

1

There's probably a conflict happen between jQuery and Wordpress here, try to do:

jQuery(function($) {
   $("form").submit(function( event ) {
        if($('#somecheckbox').is(':checked')) {  
            alert( "Handler for .submit() called." );
        }  
        else {  
            event.preventDefault();
        }
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that made it work. I see what you did now. I have worked on this for over an hour and searched all over for an example. I "thought" it should be possible. Thank you Felix.
0

Perhaps you wanted to do like this:

jQuery(function($) {
    $("form").submit(function( event ) {
        event.preventDefault();
        if($('#somecheckbox').is(':checked')) {  
            alert( "Handler for .submit() called." );
        }  
        else {  
            return false;
        }
    });
});

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.