0

I have a few different functions that set a variable if the user clicks on a certain button. It then passes that variable on to an AJAX function....except it doesn't pass the variable.

Here is an example:

$('#Button1').on('click', function(e) {
    var add1 = 'yes';
    e.preventdefault();
})
$('#Button2').on('click', function(e) {
    var add1 = 'no';
    e.preventdefault();
})
$('#DoneButton').on('click', function(e) {
    submitLink = 'submit.php'
    dataString = 'add=' + add1;
    $.ajax({
       type: "POST",
       url: submitLink,
       data: dataString,
       cache: false,
       success: function(data) {

           /* My Success Notification Here */
       }
    });
    e.preventdefault();
})

I also tried declaring the variable outside of the function as:

var add1;

That doesn't work either, just comes up as 'undefined'

What can I do to fix this?

4
  • 1
    because adding the var makes it locally scoped to the onclick Commented Jun 14, 2016 at 18:24
  • What can I do to make it so all the other functions can fetch it? @epascarello Commented Jun 14, 2016 at 18:25
  • don't use var ... Just set it .. Myvar = 'blah'; Commented Jun 14, 2016 at 18:26
  • Wow...didn't think it was that easy haha. Thanks! That fixed my issue @epascarello Commented Jun 14, 2016 at 18:29

3 Answers 3

3

drop the var inside so the variable reference is not locally scoped to the click.

var add1;
$('#Button1').on('click', function(e) {
  add1 = 'yes';
  e.preventdefault();
})
$('#Button2').on('click', function(e) {
  add1 = 'no';
  e.preventdefault();
})
$('#DoneButton').on('click', function(e) {
  submitLink = 'submit.php'
  dataString = 'add=' + add1;
  $.ajax({
    type: "POST",
    url: submitLink,
    data: dataString,
    cache: false,
    success: function(data) {

      /* My Success Notification Here */
    }
  });
  e.preventdefault();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, didn't think it was that easy haha. This fixed my issue. Thanks!
0

Putting var add1 in the function declares it locally in that function. If you want to declare it outside, you have to add var add1; outside the function, and then remove the vars inside the function, ie `add1 = 'no';

Comments

0

You need to define the variable in the shared scope so that all the functions can access it, in this case that's the global scope. Then assign a value to it inside the local scope of the function.

The simple part you were missing is that once you've declared it in the shared scope using var myVar, you don't need to use the var keyword again when assigning to it, because doing so creates a different variable only accessible in the local scope of the function, regardless of whether it has the same name.

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.