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

  • Home
  • SEARCH
  • 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 7747497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:32:41+00:00 2026-06-01T10:32:41+00:00

I have a Ajax request that fires on clicking a href that is part

  • 0

I have a Ajax request that fires on clicking a href that is part of a set of href’s having the same class .button. The HTML looks like this:

<div id="button-container">
    <a href="#" class="button red" data-id="1"></a>
    <a href="#" class="button red" data-id="2"></a>
    <a href="#" class="button red" data-id="3"></a>
    <a href="#" class="button red" data-id="4"></a>
</div>

Since, the href’s having the class .button are themselves loaded via a separate ajax call into the div #button-container and not available during page load, I am using jQuery on() to fire ajax call on clicking the .button href’s. The javascript looks like this:

jQuery('#button-container').on('click', '.button', function(e){
    e.preventDefault();

    var id = jQuery(this).data('id');

    jQuery.ajax({
        url: ajaxVars.ajaxurl,
        type:'POST',
        async: true,
        cache: true,
        timeout: 10000,
        data: 'action=button_action&id=' + id,
        success: function(value){
            if (parseInt(value) == id) {
                jQuery(this).removeClass('red').addClass('green');
            } else {
                jQuery(this).removeClass('green').addClass('red');
            }
        },
        error: function() {
            //alert(error);
        }
    });
});

The ajax fires as desired and I get a response from the server upon success. On success I need to toggle the class representing the color of href .button from .red to .green and vice-versa. This is where I have the issue. I get an error a.ownerDocument is undefined in the console. This appears to happen when removing the class. Also, I need to “only toggle the class of the clicked href”, hence I am using jQuery(this).

Any ideas on how to individually toggle the class of the href’s in above scenario?

Regards,
John

  • 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-01T10:32:42+00:00Added an answer on June 1, 2026 at 10:32 am

    Inside your .ajax() success callback, this is not the anchor. You can force it to be the clicked anchor by passing a context property as an option:

    jQuery.ajax({
         url: ajaxVars.ajaxurl,
         type:'POST',
         async: true,
         cache: true,
         timeout: 10000,
         data: 'action=button_action&id=' + id,
         context: this,    // <-- HERE
         success: function(value){
             if (parseInt(value, 10) == id) {
                 jQuery(this).removeClass('red').addClass('green');
             } else {
                 jQuery(this).removeClass('green').addClass('red');
             }
         },
         error: function() {
             //alert(error);
         }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a drop-down form that fires up an AJAX request
I have a AJAX request that will return a string that contains some HTML
I have a webpage that fires off an Ajax request which stores some data
I have a jQuery UI Autocomplete control that fires an Ajax request when minLength
I have ajax request that do 3 missions: Save Model (DB) Send Email Give
Is it possible that using jQuery, I cancel/abort an Ajax request that I have
I have an application that uses Ajax.Request and its onSuccess event handler in lots
I have a tooltip on an anchor element, that sends an AJAX request on
Situation: I send an ajax request that returns HTML containing elements needing event handlers
I have a very simple javascript class that does an ajax call to a

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.