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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:01:25+00:00 2026-05-25T22:01:25+00:00

I got an if-else script: $(‘#favitem’).live(‘click’, function () { var fid = $(this).parent().attr(‘id’); if

  • 0

I got an if-else script:

     $('#favitem').live('click', function () {
        var fid = $(this).parent().attr('id');

        if (isFav(fid)) {
            alert("Do you want to remove from favorite?");
        }
        else {
            alert("Add to favorite?");
        }
    });

calling the isFav script function:

    function isFav(fid) {

    $.ajax({

        url: '/Stock/IsFav',
        type: 'GET',
        data: { id: fid },
        success: function (result) { return result; }
    });
}

which in turn calling my controller action:

    public Boolean IsFav(int id)
    {
        var food = dbEntities.FOODs.Single(f => f.FoodID == id);
        if (food.FavFlag == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Everything seems works fine, I get a true from firebug, BUT i get the alert message from the else statement. The if statement just never being entered.
I cant get what is wrong here. Any idea?? Please help..

  • 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-25T22:01:25+00:00Added an answer on May 25, 2026 at 10:01 pm

    The ajax request in isFav is async and the isFav method will return before it is completed.

    This is probably how I would solve it:

    function isFav(fid, callback) {
        $.ajax({
            url: '/Stock/IsFav',
            type: 'GET',
            data: { id: fid },
            success: function (result) { callback(result); }
        });
    }
    
    
     $('#favitem').live('click', function () {
        var fid = $(this).parent().attr('id');
    
        isFav(fid,function(result){
            if(result && result.toLowerCase() == "true"){
                alert("Do you want to remove from favorite?"); 
            } else {
                alert("Add to favorite?");  
            }
        });
    });
    

    You want to make sure that the code in the if-block is run after the ajax request is done. In this snippet this is solved by the callback method that is called when the ajax success function is executed.

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

Sidebar

Related Questions

This is how far I got: <head> <script type=text/javascript> function showonlyone(thechosenone) { var article
I got this piece of script (runned locally): <script> last = 0; function uploadnew(){
Has anyone else run into this problem before? I've got a method that calls
I got this colored dir script from http://tasteofpowershell.blogspot.com/2009/02/get-childitem-dir-results-color-coded.html : function ls { $regex_opts =
I've got this SQL script that needs to exist in two places in source
I've finally got this PHP email script working (didn't work on localhost…), but my
I was trying this script from a pdf file.I got stuck where the target
This is the calendar script I got from an online tutorial. It works fine
So I got stuck in this dead end. I have AJAX script and jQuery
All, I've got the following code: <script type=text/javascript> function Store( lat, long, text )

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.