Answer the question
In order to leave comments, you need to log in
How to access a variable inside a function in jquery?
I do calculations and in one condition I need to make a request to the database
$.post(
"/calc/sticker/get-options", {},
function (options) {
$options = JSON.parse(options);
});
}
Answer the question
In order to leave comments, you need to log in
It is better to do the replacement with regular expressions so that all occurrences are replaced at once, and not one at a time.
Which bb-code to which class to change is easier to set with a dictionary object.
Something like this:
// словарь bbcode: className
const dict = {
'info': 'info-text',
'admin': 'admin-text',
};
var tds = document.getElementsByTagName("td");
for (let k = 0; k < tds.length; k++) {
let td = tds[k];
if (td.className !== 'posttdMessage') continue;
let s = td.innerHTML;
for(let bb in dict) {
let re_open = new RegExp('\\[' + bb + '\\]', 'ig');
let re_close = new RegExp('\\[/' + bb + '\\]', 'ig');
s = s.replace(re_open, '<div class="' + dict[bb] + '">').replace(re_close, '</div>');
}
td.innerHTML = s;
}
one option is to make this variable global.
well, the closure, in fact, is the closure for that, in order to work out and forget.
although I could be wrong.
options = $.post( "/calc/sticker/get-options", {});
$options = JSON.parse(options);
Maybe so?
var $options; // глобальная переменная
$.post( "/calc/sticker/get-options", {}, function (options) {
$options = JSON.parse(options);
});
console.log($options); // вывод в консоль результатов
var $options;
. But you should always remember that you can overwrite data by accident. var container = {
options:{},
pictures:[],
selected:"element_name",
// другие данные
};
container.options = JSON.parse(options); // присвоение
//получение данных
var $options = container.options;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question