simo9900 simo9900 - 1 month ago 9
Ajax Question

exctracting a variable from a jquery method

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.

Answer

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. response should then be available in the same scope as your alert call.