0

I want to access the returned boolean of my function on another function.

$('.asset_edit').change(function(){
        var asset_id=$(this).attr('id');
        var asset=$("#"+asset_id).val();

        var dataString = "device="+device+"&asset="+asset;

        $.ajax({
            type: "POST",
            url: "<?php echo site_url('c_device/check_assetCode'); ?>",
            data: dataString,
            cache: false,
            success: function(html){
                if(html)
                    return true;
                else
                    return false;
            }
        });
    });

Is there a way where I could access the "return true or return false" on my $(".edit_tr").click(function() function?

In Javascript you just call the certain function name but in jQuery, I have no idea how to.

3
  • 1
    Ah, the question asked 100 times a day. It can not be done because the Ajax call you are using is asynchronous. Commented Feb 18, 2014 at 15:04
  • You can change a variable asynchronously, the variable will be changed when the success event is fired. Commented Feb 18, 2014 at 15:06
  • You can see my answer and example Commented Feb 18, 2014 at 15:15

2 Answers 2

1

You can use global variable;

var global_status;
$('.asset_edit').change(function(){
        var asset_id=$(this).attr('id');
        var asset=$("#"+asset_id).val();

        var dataString = "device="+device+"&asset="+asset;

        $.ajax({
            type: "POST",
            url: "<?php echo site_url('c_device/check_assetCode'); ?>",
            data: dataString,
            cache: false,
            success: function(html){
                if(html) {
                   global_status="true";
                   return true;
                }
                else {
                    global_status="false";
                    return false;
                }
            }
        });
    });

$(".edit_tr").click(function() {
   alert(global_status);
})

Here is a working demo: http://jsfiddle.net/vWkjr/1/

While testing, first click edit and see undefined, and then select value from selectbox and click edit.

Sign up to request clarification or add additional context in comments.

4 Comments

But first, you need to trigger change event. First trigger that and global_status will be set.
This is the right way to go, but I would recommend some validation in the click event. Perhaps check if the value is set, and if not alert "waiting for ajax to complete, try again later"
@epascarello: I upvoted, and nobody is suggesting you can return a value to the original calling function. This solution provides a method to set a result value that can be accessed from another method - which is what is being asked
@Mel Sy Gallosa I have added js fiddle for example
1

insted of using return value you can add a class specifying html or not to the element it self. or you can declare a global variable and assign the value to it.

$('.asset_edit').change(function(){
    var asset_id=$(this).attr('id');
    var asset=$("#"+asset_id).val();

    var dataString = "device="+device+"&asset="+asset;

    $.ajax({
        type: "POST",
        url: "<?php echo site_url('c_device/check_assetCode'); ?>",
        data: dataString,
        cache: false,
        success: function(html){
            if(html)
                $(this).addClass('ishtml');
            else
                $(this).addClass('nothtml');
        }
    });
});

And this class can be accessed with in other functions. like

function somefunction(){
    if($('.asset_edit .ishtml ').length){
          //true then do something
      }else{
          //false then do something
      }
}

1 Comment

It would be more helpful if you provided an example solution along with your theory. That would make for a more complete and useful answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.