1

I tried to pass the value of variable from one function to another

function changeParent(){
    $(document).ready(function(){
        $('a').on('click',function() {
            var x = $(this).attr('id');
            var y = $(this).attr('name');
            $.ajax({
                type: 'POST',
                url: 'http://test.local/Family.php?action=getId',
                data: {'childId' : y},
                success: function(msg) {
                    document.getElementById('names').value = x;
                    msg = x ;
                }
            });
        });
    });
}

The second function that calls msg from the first function

$(document).ready(function(){
    $('#saveId').on('click',function() {
        alert(msg);        
    });
});

I tried

alert(window.msg);

but it didn't get the last value in msg. How I can pass it?

5
  • 1
    $('#saveId').on('click',function() {alert(document.getElementById('names').value); }); Commented Jun 19, 2018 at 7:11
  • msg is scoped to the first function, define it globally first. Right at the top just do var x; Commented Jun 19, 2018 at 7:13
  • @RickCalder How i can make it global .. i tried window.msg but its not work Commented Jun 19, 2018 at 7:20
  • @Adelin i tried alert(document.getElementById('names').value); its not work :/ Commented Jun 19, 2018 at 7:21
  • Check all 3 answers lol Commented Jun 19, 2018 at 7:21

3 Answers 3

3

Declare a variable outside of the two functions' scope. There is a msg in the success calback function, so when you were calling msg = x it actually assigned x to that reference.

var message;

function changeParent(){
   $(document).ready(function(){
        $('a').on('click',function() {
            var x = $(this).attr('id');
            var y = $(this).attr('name');
            $.ajax({
                type: 'POST',
                url: 'http://test.local/Family.php?action=getId',
                data: {'childId' : y},
                success: function(msg) {
                    document.getElementById('names').value = x;
                    message = x ;
                }
            });
        });
    });
}

$(document).ready(function(){
    $('#saveId').on('click',function() {
        alert(message);        
    });
});

This way the variable message is available for both functions.

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

1 Comment

Beat me to it :)
1

msg is scoped to the first document ready function and is not available to the second function.

<button id="btn1">
Button 1
</button>

<button id="btn2">
Button 2
</button>

Notice how msg1 is only defined inside the first document ready function, but msg2 is defined globally (not inside a function). Button 1 won't work, Button 2 will, check the fiddle at the end.

var msg2
$(document).ready(function(){
  var msg1 = "Hello World"
  msg2 = "Goodbye World"

})

$(document).ready(function(){

  $("#btn1").on("click", function(e) {
    e.preventDefault()
        alert(msg1)
  })
  $("#btn2").on("click", function(e) {
    e.preventDefault()
        alert(msg2)
  })
})

Fiddle: http://jsfiddle.net/tnj4yLeg/3/

Comments

1

Problem is msg . Your msg in success callback is only local variable also strange is :

success: function(msg) {
     document.getElementById('names').value = x;
     msg = x ;
  }

// No sense ! msg is arg for callback func

Make global variable :

var Message  = '';

Intro success :

Message = msg;

Comments

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.