0

I want to extract the variable response from the mentionsInput() method and using it outside this method, but when I try an alert() on this variable it's empty.

jQuery(document).ready(function() {  
    var choix = $('#choixaide').val();
    var choix_sous_theme1 = $('#choix_sous_theme1aide').val();

    $('textarea.mention1').mentionsInput('val', function(text) {                
        var response = text;
    });

    alert(response);
});

Thanks for your help.

jacefarm
  • 6,747
  • 6
  • 36
  • 46
simo9900
  • 133
  • 1
  • 9
  • Put the `alert()` inside the function? – Rory McCrossan Oct 20 '16 at 15:28
  • Uhh, why not `var response = $(el).val(); alert(response);` what's the callback for ? – adeneo Oct 20 '16 at 15:29
  • I think the problem is you are declaring the variable response inside the method mentionsInput. Try to use a clousure, declare the variable response outside the method and assign it inside it. I'm not confident this will work but that's a try. – Pievis Oct 20 '16 at 15:31

2 Answers2

1

As you have it now, response is only available within the scope of your mentionsInput method, but not outside of it.

Additionally, when running your code, I see the following error:

Uncaught TypeError: $(...).mentionsInput is not a function"...

Are you sure you've properly loaded the jquery.mentionsInput UI component? You'll need to solve for this error first, if you are also encountering this.

Then, you'll need to declare the variable response prior to and outside of your mentionsInput method, and then set it within mentionsInput. The value set for response should then be available in the same scope as your alert call.

jacefarm
  • 6,747
  • 6
  • 36
  • 46
0

I think this should do the trick:

jQuery(document).ready(function() {
          var choix = $('#choixaide').val();
          var choix_sous_theme1 = $('#choix_sous_theme1aide').val();

          var response = $('textarea.mention1').val();

          alert(response);

        });
ShaneDaugherty
  • 427
  • 4
  • 7