Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi.

In my MVC project I have a button that has to trigger a script.
As it happens the page doesn't listen to the event.

I put the script in my view where the form is.

this works
<script >
              alert("it worked");
</script>





this doesn't work
<script >
         $(document).ready(function () {
            $("#btnSubmit").click(function () {
                alert("it worked");
</script>



this doesn't work
<script >
         $(window).load(function () {
            $("#btnSubmit").click(function () {
                alert("it worked");
</script>






UPDATE:

this works
<script>
    $(document).ready(function () {
        alert("mmm");
    });
                </script>

so it's the click event that's not working



Can't find any solution on the net

Thanks in advance
Posted
Updated 2-Oct-15 9:24am
v2

That is because the code that works and the code that doesn't work are not equal. They have different events in which they would trigger. For example, the first piece of code,

JavaScript
alert('it worked');


Would not need any event to happen, in order to alert the user. It would execute as soon as browser hits the line of code. But, the other cases have an event that needs them to trigger. Perhaps you are novice to jQuery?

JavaScript
// Assuming the document ready function
$('#btnSubmit').click(function() {
   alert('it works too');
});


But the above code to work requires that you actually click on that button. Also, you need to close the brackets, why did you leave the door open?

Those lines of code that are under the events in jQuery[^], do not execute (same as JavaScript; the event functions are executed once that event takes place). To execute them, you need to perform the event (on the same element).

Also, you may not understand what a $(document).ready(function() { }) handler is. It is a function, that executes once the document object in jQuery is ready. Then the function to execute is where you write the code, the code then attaches the jQuery function handlers to jQuery objects.

JavaScript
var obj = document.getElementById("elId"); // JavaScript object
var jObj = $('#elId');                     // jQuery object


They have different functions on them, based on the object type. I would recommend that you learn more about .ready()[^] function. Same applies to $(window).load(function () { }), read their documentations.

Edit

As from your edit, it seems as if the problem was closing brackets. Try this,

JavaScript
$(document).ready(function () {
    $("#btnSubmit").click(function () {
        alert("it worked");
    });
});


This should work, if you click on the button with that ID.
 
Share this answer
 
v3
SOLVED


just had to put an id="btnSubmit" on the button (i had it as name="btnSubmit")
 
Share this answer
 
Comments
Patrice T 2-Oct-15 16:21pm    
Please delete this as it is not an answer.
This can be a question improvement or a comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900