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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:22:51+00:00 2026-05-26T16:22:51+00:00

Alright, so I have a button that when clicked, will change the class of

  • 0

Alright, so I have a button that when clicked, will change the class of a given div. The thing is, I only want this to happen if a variable (answer) does not contain the string “No”. This works… but the problem is that even if the variable doesn’t contain the word “No”, the code won’t execute. The ajax result contains either “No User ID” or “User ID:#”.

$(document).ready(function() {

$('.like').on('click', function() {
    postID = $(this).attr('id').replace('like_', '');

    // Declare variables
    value = '1';

    myajax();

    return false;

    var doit = answer.IndexOf("No");
    if (doit < 0){
        $('#post_' + postID).removeClass('dislike').addClass('like');
    }

});

function myajax(){
    // Send values to database
    $.ajax({
        url: 'check.php',
        //check.php receives the values sent to it and stores them in the database
        type: 'POST',
        data: 'postID=' + postID + '&value=' + value,
        success: function(result) {
            answer = result;
            $('#Message_' + postID).html('').html(result).prependTo('#post_' + postID);
        }
    });
}
});

SO I know my problem lies with the var doit. I just can’t figure out what’s wrong with it.

  • 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-26T16:22:51+00:00Added an answer on May 26, 2026 at 4:22 pm

    the success function is executing after the call to myajax returns

    the first ‘a’ in ajax stands for asynchronous, so you want to move the entire block of code into the success function, likeso:

    $(document).ready(function() {
    
    $('.like').on('click', function() {
        postID = $(this).attr('id').replace('like_', '');
    
        // Declare variables
        value = '1';
    
        myajax(function(answer) {
               if (answer.indexOf("No") < 0){
                  $('#post_' + postID).removeClass('dislike').addClass('like');
                }
        });
    
        return false;
    
    });
    
       $('.dontlike').click(function() {
         myajax(function(answer) { alert('check.php returned:' + answer); }
       });
    
    function myajax(onSuccess){
        // Send values to database
        $.ajax({
            url: 'check.php',
            //check.php receives the values sent to it and stores them in the database
            type: 'POST',
            data: 'postID=' + postID + '&value=' + value,
            success: function(result) {
                $('#Message_' + postID).html('').html(result).prependTo('#post_' + postID);
                onSuccess(result);
            }
        });
    }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright so I have this C++ image capturing class. I was wondering if I
Alright, currently I have my SWF hitting a php file that will go and
Alright. I have a query that looks like this: SELECT SUM(`order_items`.`quantity`) as `count`, `menu_items`.`name`
Alright, so I have a query that looks like this: SELECT `orders`.*, GROUP_CONCAT( CONCAT(
Alright I have an xml document that looks something like this: <xml> <list> <partner>
Alright so, I have something that looks like this : for (j = 0;
Alright...I've given the site a fair search and have read over many posts about
Alright, I'll preface this with the fact that I'm a GTK and Python newb,
Alright, I'll try to explain this the best I can. I have an iPhone
I have a view that I'm loading, and my onCreate() method looks like this:

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.