Click here to Skip to main content
15,908,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using jquery just to display an alert box on button's click event. But when i click on button, nothing happens. I also tried to display alert on document.ready. But it does not work.

Here is my code - this is the view. I am using MVC structure and a master page.
HTML
<table style="width: 100%;">
     <tr>
     <td colspan="2"> Search City wise </td>
     </tr>
         <tr>
             <td>
                From
             </td>
             <td>

             </td>

         </tr>
         <tr>
             <td>
            To:
             </td>
             <td>
               <input id="txtTo" type="text" />
             </td>
         </tr>

   <tr>
   <td>
   <input id="btnSearch" type="button" value="Go" />
   </td>
   </tr>
     </table>


Jquery code :

JavaScript
$(document).ready(function () {

    alert("dsdsdsd");
   
    $('#btnSearch').click(function () {
        alert("button clicked");
      
        });
    });
    $('#Text2').hide();
});
Posted
Comments
ZurdoDev 3-Mar-15 11:51am    
Because you are using a master page the id that is actually generated is probably not btnSearch. Run your page and then view source to see what the actual ID is.

1 solution

This is what your code currently does:
JavaScript
$(document).ready(function () {
 
    alert("dsdsdsd");
   
    $('#btnSearch').click(function () {
        alert("button clicked");
      
        }); // this closes the click event handler
    }); // this closes the ready event handler
    $('#Text2').hide();
}); // this attempts to close something that doesn't exist

That causes a syntax error.

I assume you want the last }); to close the 'ready' event handler. Remove the }); below the "button clicked" line. Then your code will do this, which will work fine:
JavaScript
$(document).ready(function () {
 
    alert("dsdsdsd");
   
    $('#btnSearch').click(function () {
        alert("button clicked");
    }); // this closes the click event handler
    $('#Text2').hide();
}); // this closes the ready event handler
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 3-Mar-15 12:11pm    
Good catch, a 5.
—SA
Thomas Daniels 3-Mar-15 12:11pm    
Thank you!

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