How to Bind a Click Event in Ajax

Ajax provides you with a way to query a server using JavaScript without the need to refresh the Web page. Programmers often use Ajax to query the server when a user clicks a button or image. You must link the Ajax function to the "onclick" event for the button or image to execute the code in the user's browser.

Instructions

    • 1

      Open Windows Explorer or your preferred file manager and navigate to the HTML file that contains your Ajax queries. Right-click on the file, click "Open With" and select your Ajax editor in the list of programs.

    • 2

      Add the Ajax classes to your code. Type or copy and paste the following code between the opening and closing <head> tags in the document:

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

    • 3

      Create a button or image for the user to click that will trigger the Ajax code. The following code creates a button for the Ajax trigger:

      <input type="button" id="ajaxtrigger" value"Click Here">

    • 4

      Create an Ajax function that processes code when the user clicks the button. The example below displays a message to the user when the button is clicked:

      $('#ajaxtrigger').click(function() {
      alert('You clicked the button.');
      });

      Notice the ID of the button is used in the definition of the function.

Related Searches:

References

Comments

Related Ads

Featured