views:

37

answers:

4

i create this tiny JS so i can control the form submit with jQuerty

$('#submit').click(function() { 
  $('#addForm').submit();

});

can i use a simple href link for it? something like

<a href="javascript:$('#addForm').submit();">link</a>

i try

<a  href="javascript:document.addForm.submit();">link</a>

bot it dind't work (it by pass the other jQuerty in the page)

A: 

Never ever put JavaScript in the href attribute. If you must put in the HTML, use the onclick attribute, and rememter to add return false. Also, never ever prefix script with javascript:.

Your first script block should do the job just as fine as well, though.

(And what does "it by pass the other jQuerty in the page" mean?)

Matti Virkkunen
+1  A: 

If you want the JS that you created to control the submit, don't have a href value in your link:

<a id="submit">link</a>
Clicktricity
A: 

You can attach to the click event with jquery. It's also cleaner not to put any javascript in your html.

<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(function(){
        $('#aSubmit').click(function(){
            $('#form1').submit();
            return false;
        });
    });
</script>
<style type="text/css"></style>
</head>
<body>
    <form id="form1" action="http://www.google.com/search" name=f>
        <input name="q" />
    </form>

    <a href="#" id="aSubmit">submit</a>
</body>
</html>
Dan
i try to avoid a new function in the code
@user186585 Why?
alexn
A: 

You cannot submit your form that way with a link and some javaScript, even jQuery. You have to use an XHR call to submit your query AND not refresh the page. (you can submit your form with a link as presented by Dan, but I understand that you want to do more than just that from your questions) The reason being that the "return false" statement will impact the link action, and not the form submission itself. In any case, I would advise you to not use a link to submit your form at all.

You can easily submit a form with the normal submit button and a bit of jQuery. That way, you provide a nice fallback to your users that have not enabled javaScript.

You can even submit the form with some client side validation, using the following HTML / javaScript:

<!DOCTYPE html>
<html>
<head>
    <title>Sample</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
    <script type="text/javascript">
        $(function(){
            $('#addForm').submit(function(){
                validate();
                ajax_submit_function_taking_form_data_as_argument($(this).serialize());
                return false;
            });
        });

        function validate(){
            //Do some client side validation if required
        }

        function ajax_submit_function_taking_form_data_as_argument(theData){
            $.ajax({
                type: 'GET',
                url: "http://search.google.com/",
                data: theData,

                error: function(e){
                    alert('error...');
                },
                success: function(data){
                    alert('success!');
                }
            });
        }
    </script>
    <style type="text/css"></style>
</head>
<body>
    <form id="addForm">
        <input type="text" name="field1" value="" />
        <input type="submit" value="submit" />
    </form>
</body>
</html>

A last solution, albeit maybe too heavy weight for your use, would be to use the excellent jQuery form plugin from http://jquery.malsup.com/form/

I hope that helps!

ekynoxe