3

How can I make my form run a function when submit is clicked?

<form id="commentForm" name="comment">
    <fieldset>
        <label for="name">Name <span>(required)</span></label>
        <input type="text" class="text" id="name" value="" />
        <label for="email">Email <span>(will not be published) (required)</span></label>
        <input type="text" class="text" id="email" value="" />
        <label for="website">Website</label>
        <input type="text" class="text" id="website" value="" />
        <label for="message">Message <span>(required)</span></label>
        <textarea id="message" class="textarea" rows="10"></textarea>
        <input type="submit" name="submit" class="submit" id="submit_btn" value="Submit Comment" action="JavaScript: ajax_add_comment();">
    </fieldset>
    ...

I am trying running the following function:

function ajax_add_comment () {
    alert ("testing");
}
1

3 Answers 3

5

Use onclick attribute instead of action.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use jQuery, and use the .submit() function. You can give the form an id and then attach the submit function to it.

Example:

<form id="execute"....

</form>

<script type="javascript">
$("#execute").submit(function(){
 alert("i've submitted this form");
});
</script>

make sure you have included the jquery js file.

<script src="http://code.jquery.com/jquery.min.js" type="text/javascript"></script>

Comments

1

You can use the onsubmit event to execute JavaScript code when the form is submitted. For example:

<script>
function ajax_add_comment () {
  alert ("testing");
}
</script>

<form id="commentForm" name="comment">
                            <fieldset>
                                <label for="name">Name <span>(required)</span></label>
                                <input type="text" class="text" id="name" value="" />
                                <label for="email">Email <span>(will not be published) (required)</span></label>
                                <input type="text" class="text" id="email" value="" />
                                <label for="website">Website</label>
                                <input type="text" class="text" id="website" value="" />                                                                
                                <label for="message">Message <span>(required)</span></label>
                                <textarea id="message" class="textarea" rows="10"></textarea>
                                <input type="submit" name="submit" class="submit" id="submit_btn" value="Submit Comment" onsubmit="ajax_add_comment();">
                            </fieldset>

Thank you!

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.