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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:03:45+00:00 2026-05-30T16:03:45+00:00

So I have a table with multiple publish and unpublish buttons. Simple buttons: <button

  • 0

So I have a table with multiple publish and unpublish buttons.

Simple buttons:

<button id="publish"
    <?php
        if ($singleBlog->isPublished())
            echo ' class="green small"';
        else
            echo ' class="green red small"';
    ?>
    value="<?php echo $singleBlog->getBlogId(); ?>">publish</button>

to add the buttons.

Then I do the ajax call that return something and changes toggle the button class and name.
It used to work before I added the ajax but I cant seem to be able to make it work with the ajax call.

I suspect it is cause the this in the ajax call is refering to it and I cant seem to figure out how to get the parent of that!

Any help would be appreciated on this one.

<script>
      $(document).ready(function() {
          $("button#publish").click(function() {
            //alert($(this).attr("value"));
            var id = $(this).attr("value");
            $.ajax({
                type: "POST",
                url: "ajax/blogPublishUnpublish.php",
                data: "id="+ id ,
                success:function(result){
                    var button = this;
                    if (result == '0' || result == '1' ){
                        alert("in");
                        $(button).toggleClass("red");

                        if( $(this).is('.green') )
                            $(this).text('publish');

                        if( $(this).is('.red') )
                            $(this).text('unpublish');
                        alert("done");
                    }

                }
            });
            // alert(index);


      });
      });
      </script>
  • 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-30T16:03:46+00:00Added an answer on May 30, 2026 at 4:03 pm

    I would select the button again, or if you’re worried about switching the ids, make a variable.

     $(document).ready(function() {
          $("button#publish").click(function() {
            var myID = "#" + (this).attr("id");
            var id = $(this).attr("value");
            $.ajax({
                type: "POST",
                url: "ajax/blogPublishUnpublish.php",
                data: "id="+ id ,
                success:function(result){
    
                    if (result == '0' || result == '1' ){
                        alert("in");
                        $(myID).toggleClass("red");
    
                        if( $(myID).is('.green') )
                            $(myID).text('publish');
    
                        if( $(myID).is('.red') )
                            $(myID).text('unpublish');
                        alert("done");
                    }
    
                }
            });
            // alert(index);
    
    
      });
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple table rows that has a set of radio buttons on each
I have a table with multiple rows and columns populated by php and mySQL.
I have a table with multiple <tr> . Some of them has set class
I have div having class name .modalPopup inside the div I have multiple table
I have a table with multiple rows, with each row having a button within
I have a table with multiple rows. Each row has a check button. How
I have a table with multiple rows that all have the same class. When
i have table question (mysql with php) which contains question,category(multiple) and subcat(multiple) its belong.
I have a table with multiple rows. Each row is a form. I want
I have a table with multiple rows in each row is a select list

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.