2

I am trying to learn some JQuery. I am using an external js to write my code in. It works fine when I call the 2 paragraph functions, but when I call the submit button it just load the page again. My code looks like this:

<html>
<body>

    <p id="paragraf">This is an internal paragraf</p>


    Another Example:
    <p id="paragraftest">This is the external js script</p>

    <form>
        <input type="text" name="Submit_test">
        <button id="buttontest">Submit</button> 
    </form>
    <script type="text/javascript" src="jqueryScript/jquery.js"></script>
    <script type="text/javascript" src="jqueryScript/hide.js"></script>
</body>
</html>

Javascript:

$('#paragraf').click(function() {
    $('#paragraf').hide();
});

$('#paragraftest').click(function() {
    $('#paragraftest').hide();
});

$('buttontest').submit(function() {
    alert("test");
});

/* I have tried with "" and '' in buttontest and test, but still the same */ I have also tried with this, but it is still the same.

<form>
        <input type="text" name="buttontest">
        <input type="submit" value="">
    </form>

Can anybody see why nothing happens? Best Regards Mads

5 Answers 5

5

Missing id selector - buttontest is the id of the button so you need to use id-selector to select it(prefix with #)

$('#buttontest').submit(function() {
    alert("test");
});
Sign up to request clarification or add additional context in comments.

5 Comments

Funny, but this answer is totally incorrect. #buttontest is a button and not a form, it doesn't have a submit event at all.
Perfect... thanks a lot everybody. This solved the answer :-)
@user1883095 In your HTML buttontest is a button. If you move this id to a form this will be correct.
The good thing is that if you give a form an id, then you can have a lot of forms on the page and functions to those forms in your js, or?
@user1883095 there is nothing wrong with giving the form an id.... only thing to keep in mind is ID of an element must be unique in a page
1

Correct this syntax:

$('input[name=buttontest]').submit(function() {
    alert("test");
});

Using a unique ID for the button:

$('input#button_id').submit(function() {
    alert("test");
});

Comments

1

You should select the button with the following way:

$('#buttontest')

You hadn't used the #, which is required when we are selecting elements form the DOM using their ids. For sure it was a writing error, since you use this correctly on your other selections.

Comments

0

Submit event is a form event, and you are trying to bind it to a button.

$('form').submit(function() {
    alert("test");
});

In generally if you want to submit a form you should use dedicated onsubmit form event, not click event on any button.

Comments

0

It works with this code now:

<form id="form">
        <input type="text" name="buttontest">
        <input type="submit" value="submit"> 
    </form>
    <form id="form_1">
        <input type="text" name="buttontest">
        <input type="submit" value="submit"> 
    </form>

Javascript:

$('#form').submit(function() {
    alert("test");
});

$('#form_1').submit(function() {
    alert("test");
});

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.