0

my button doesn't work when I click on it nothing happens. This is my HTML for it:

<form id="myform" name="myform" action="senden.php" method="get">
        <ul><div>
                <li>
            <button id="testbutton" name="testbutton" value="abgesendet2" type="button">test</button>

            <p> Felder mit einem Stern müssen ausgefüllt werden! </p>
        </ul>

    </form>

and this is my jQuery for it (the button is in a form):

<script> 
        jQuery('document').ready(function(){

            $("#myform").on('click', '#testbutton', function(){
                alert("test");
            });

        });
    </script>
10
  • Where is your <form id="myform" ..> ? Please click the <> and add the rest of the relevant HTML. Commented Feb 20, 2017 at 13:37
  • please add your full HTML so that we can help\ Commented Feb 20, 2017 at 13:39
  • 1
    Check here Commented Feb 20, 2017 at 13:39
  • 2
    Use $("#testbutton").on('click', function(){ Commented Feb 20, 2017 at 13:39
  • 1
    Your code works. Which error are you getting? Are you including jquery? Commented Feb 20, 2017 at 13:44

2 Answers 2

1

You only need to make a small change. Attach the click directly to the target button and it will work.

$('document').ready(function(){
    $("#testbutton").on('click', function(){
        alert("test");
    });
 });


 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="testbutton" name="testbutton" value="abgesendet2" type="button">test</button>

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

4 Comments

how do I attach it directly to the button?
This line in my code form above does just that. Target directly the testbutton: $("#testbutton").on('click',function(){
ok, then you need to check if you get any errors in the console, in Chrome - developers.google.com/web/tools/chrome-devtools/console You should be able to see if, by any chance the jQuery library is missing.
This is no different than what the OP is doing, so why make the change?
0

Your code seems working correctly, are you sure you were including jquery? Here is what I tested:

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

<script> 
    jQuery('document').ready(function(){

        $("#myform").on('click', '#testbutton', function(){
            alert("test");
        });

    });
</script>   


<form id="myform" name="myform" action="senden.php" method="get">
<ul><div>
    <li>
        <button id="testbutton" name="testbutton" value="abgesendet2" type="button">test</button>

        <p> Felder mit einem Stern müssen ausgefüllt werden! </p>
    </ul>

</form>

Here is a jsfiddle too. Do it works?

2 Comments

So it must work in your environment! Are you working locally? How are you including jquery?
Really? Not even a "Thank you"?

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.