0

I have some ajax code:

      $(document).ready(
  function()
  {
    $("#button1").click(
    function()
    {
      $.ajax({
      type: "POST",
      url: "update.php",
      });

    });
  });

In my html code I have 200 buttons. How can i loop buttons i this ajax code? I'm newbie of ajax and js.

I know I can copy and paste and change number of buttons but I think it's not optimalize code.

Thank's for help.

4
  • 2
    Why not use class selector? Commented Feb 3, 2015 at 16:07
  • 1
    you want set one click handler to all buttons, or what? Commented Feb 3, 2015 at 16:07
  • sounds like he wants all the buttons to trigger ajax call when clicked. He should be using a class selector or something else....not an id. Commented Feb 3, 2015 at 16:09
  • It was that simple ;)) Thanks! Now I must paste my class selector x200 times:) Lets go Commented Feb 3, 2015 at 16:16

2 Answers 2

2

You can apply some common class to the buttons to select all of them:

$(document).ready(
  function()
  {
     $(".myButtonClass").click(
    function()
    {
     $.ajax({
      type: "POST",
      url: "update.php",
      });
    });
  });
Sign up to request clarification or add additional context in comments.

Comments

0
$(document).ready(function(){
    $(".button").click(function(){
        $.ajax({
            type: "POST",
            url: "update.php",
        });
    });
});


<button class="button" id="button1">button 1</button>
...
<button class="button" id="button200">button 200</button>

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.