Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:26:57+00:00 2026-06-02T10:26:57+00:00

Trying to find out why this jQuery JS isn’t making ajax call though it

  • 0

Trying to find out why this jQuery JS isn’t making ajax call though it is being called for execution.

I have this button to make an ajax GET request to a method in the controller, the method will partial render. When I click on the button I don’t see any request coming on the console but I see the alert “test” on the browser.
I have the same exact JS with other parameters working for other tupes of ajax calls, so I just copied one of them and changed all required parameters, expecting it shall work right away. Neither I get any errors on the console. My routes and id names are good and verified. What is it that I am missing here?

view – Note: this button is rendered via a different ajax, that part works.

<%= button_tag "Add / Remove", :id => "add_remove_button", :onclick => "javascript:add_remove();" %> #note: this is buried under div tags in a html table

JS-

 function add_remove(){
$('#add_remove_button').click(function() {
      $.ajax({
        type: 'GET',
        url: "/item/add_remove", 
        success:$('#view_item').html(data)

        /*function(){  },
        data:$('#test').serialize(),
        error: function(){  },
        success: function(data){   },
        complete: function (){   }*/
        }); #No ajax call made
/*return false;*/
});
alert('test'); #I get this alert
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:26:58+00:00Added an answer on June 2, 2026 at 10:26 am

    You’ll always see that alert() because click() is asynchronous: the code inside the function() passed to click does not get executed until you click, but the rest of add_remove() will get called.

    Here’s what is actually happening in your code, which explains why the AJAX call doesn’t get made:

    1. Using :onclick => ... attaches add_remove() to your button.
    2. You click the button, add_remove() gets called and attaches another click callback to your button. Then add_remove() calls alert(). There is no AJAX call happening here, just adding a new click handler, and sending an alert.
    3. You click the button a second time, and you will attach a third click callback to the button. However since you also attached a click handler the first time you clicked the button, you should see an AJAX request here.
    4. Click it a third time and you’ll see two AJAX requests this time, for a total of 3 AJAX requests.

    Here’s what you actually want to do. Remove the :onclick => … from the button:

    <%= button_tag "Add / Remove", :id => "add_remove_button" %>
    

    Attach a click event to the button when the page first loads:

    $(function(){
      $('#add_remove_button').click(function() {
        $.ajax({
          type: 'GET',
          url: "/item/add_remove", 
          success: function(data) { $('#view_item').html(data); },
          data: $('#test').serialize(),
          error: function(){  },
          success: function(data){   },
          complete: function (){   }
        });
        return false;
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find out how to read/write notations like this: jQuery.getJSON( url [,
I'm trying to find out where this function comes from. Any one have any
I'm trying to find out weather I can do this or not. I have
I've been trying to find a way for my jQuery AJAX-script to find out
I'm trying to find out how they were able to create this site. http://www.cpeople.ru
i'm trying to find out if a string value EndsWith another string. This 'other
I've been Googling my butt off trying to find out how to do this:
I have been trying to find out why the following lines of code do
I am trying to find out why jquery can't return the actual heigth of
I'm newby with Jquery and I'm trying to find out how to apply on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.