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 541469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:20:47+00:00 2026-05-13T10:20:47+00:00

I have a button that depending on a class value should send different values

  • 0

I have a button that depending on a class value should send different values to the POST however what ever happens it is only running one condition, whatever I do I can only get it to send the POST the method=add data.

This is my code

$("a.navlink").click(function (ev) {
    $(this).removeClass("active");
    var url = $(this).attr("href")
    ev.preventDefault();
    if($('a.navlink:not(.active)')) {

        $.ajax ({
             url: url,
             type: "POST",
             data: "method=add",
             success : function (html) {
                $('#accordion').accordion('destroy');
                 $("#accordion").append(html);
                 $('#accordion').accordion({
                     active: 0,
                     header:'h2.Blog'
                 });
             //alert(accordion())
             }
         });
    }
    if ($(this).hasClass("active")) {
    //  $(this).addClass('active')
    $.ajax ({
         url: url,
         type: "POST",
         data: "method=delete",
         success : function (html) {
            alert("hello")
            $(this).removeClass('active')
         //alert(accordion())
         }
     });    
    }


     });

Basically what I need is, if when the button is clicked it gets a class called active added to it and the ajax runs, when it is clicked again, the class needs to be removed and the ajax run, is this possible?

  • 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-05-13T10:20:47+00:00Added an answer on May 13, 2026 at 10:20 am

    What you are basically doing now in your first if clause is that you’re checking whether $('a.navlink:not(.active)') is true or false – $() always returns an object which evaluates to true.. You should do a hasClass on that also. And you are removing the ‘active’ class before the if, so how can you compare it later on?

    It is unclear what you’re trying to do, but what you want is probably something like this:

    $("a.navlink").click(function(ev) {
        var url = $(this).attr("href")
        ev.preventDefault();
        // If the link is not active, run 'add':
        if(!$(this).hasClass('active')) {
            $.ajax ({
                url: url,
                type: "POST",
                data: "method=add",
                success: function (html) {
                    $('#accordion').accordion('destroy');
                    $("#accordion").append(html);
                    $('#accordion').accordion({
                        active: 0,
                        header:'h2.Blog'
                    });                
                }
            });
            // Mark it as active
            $(this).addClass('active');
        // If the link is active, run 'delete':
        } else {
            $.ajax ({
                url: url,
                type: "POST",
                data: "method=delete",
                success: function (html) {
                    alert("hello")
                }
            });
            // Unmark it.
            $(this).removeClass('active');
        }        
    });
    

    That code’s purpose doesn’t make much sense to me, so tell me if I understood you wrong.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer ok, now it works: -mmacosx-version-min=10.5 make clean && ./configure CFLAGS="-arch… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer The box-shadow is not considered in the total width of… May 13, 2026 at 7:32 pm
  • Editorial Team
    Editorial Team added an answer Won't the "more relationship normalized format" destination database be a… May 13, 2026 at 7:32 pm

Related Questions

I have a class called Question that has a property called Type. Based on
I have a button in a Nib based view which does not behave like
When writing Cocoa apps, I do the majority of the user interface layout programmatically.
I'm trying to modify a C# WinForms application that uses multiple forms. At startup,
I have an Application layout that is based on a treeView at the left,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.