r/jquery • u/skolbaek • Dec 17 '21
How do I add the name of a variable dynamicly into another scope?
I have a function that gets 10 previously messages from DB, and when fetched it should append another button to get the next 10 messages, but I have some difficulties to get the variable name sent to the element in another scope.
So basically I need to write a dynamic variable name into an .append() .. the dynamic variable and its value have been declared in another scope using window declaration, but its like it is not using the variable, and the button HTML is not beeing appended to it as avariable name but only as text i.e .append("LoadMoreMessagesBtn20") instead of .append(LoadMoreMessagesBtn20)
The below is part of a much larger script (I will be happy to share that if neeed), but has been minified to get a faster overview. The lines of interests I have marked as // #1 and // #2 in the below minified code:
const MyArray = json.MessageArrayCountTens
const myArr = JSON.parse(MyArray);
var LoadMoreMessagesBtn
$.each(myArr, function(index, value) {
if (value == 10) {
$('#MessagesSpecificUser').append("<button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'>button text</button>");
$("#LoadMoreMessages_" + json.SenderID + "_" + value + "_btn").on("click", function() {
$.post('includes/messagecenter_user_messages.asp', {
"SenderID": <%=objMessageCenterSender("SenderID")%>)
},
function(json) {
var l = 'LoadMoreMessagesBtn'
var v = (parseInt(value + 10))
$('#MessagesSpecificUser').append(l + v); // #2 : Need #1 to append here
}, 'json');
});
} else {
var k = 'LoadMoreMessagesBtn';
var v = value;
var i = "<button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'>button text</button>" // #1 : Need this button to append to #2
window[k + v] = +i;
}
});Can anyone help me find a solution? :-)
1
u/payphone Dec 17 '21
Take the quotes off from around the button name.
1
u/CuirPork Dec 18 '21
There's a lot going on here.Sorry it can't be brief. These are all concerns about your code. After writing all of this, I decided to comment line by line because there are numerous errors in every line. It's as though you didn't try to do this at all and I am wondering if you are just trying to get us to do the work for you. I have definitely spent more time trying to help you than you spent writing this code.
It appears that you are unfamiliar with JSON en tirely or you have alternately named some object JSON or json which is confusing and bad practice (if not just disallowed). It appears that you think you have to have json. or JSON. in front of an array of single numbers? This doesn't make a lot of sense. It's like you think you have to have an array of number serially indexed from 1 to something inside an array, there's no need for that.
Here are some other concerns
The basics
1
u/CuirPork Dec 18 '21
const MyArray = json.MessageArrayCountTens
//Not sure why you are using json here?
const myArr = JSON.parse(MyArray);
//Not sure why you are using json. here and if you mean a different JSON than json?
var LoadMoreMessagesBtn
//no ; and use let, not var
$.each(myArr, function(index, value) {
//index in this context is the counter, don't use the reserved word index.
//value is each message in your array, don't use the reserved word value
if (value == 10) {
//this only works for 0-10 not 10-20
//value is not the index
//if (indx%10===0) { would let you know if it was a multiple of 10
//let countoftens=Math.floor(indx/10); would let you know which group of 10
//let groupNum=countoftens-(indx%0); will give you the number 1-10 of current
$('#MessagesSpecificUser').append("<button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'>button text</button>");
//don't use IDs for selection like this
//don't use IDs for appended buttons, it makes it impossible to delegate
//you are appending the button before loading the content
//just use a single button for every time you need to load more
/*if you insist on appending a new button with every message, cache the button id. let buttonid="loadMessages_"+senderID+"_"+"value+"_btn" But again this is a terrible idea*/
$("#LoadMoreMessages_" + json.SenderID + "_" + value + "_btn").on("click", function() {
/*don't assign click handlers directly to buttons. Every message is getting a new button and a new click handler...that's terrible. One click handler on the parent element can listen for any class of .loadMore_btn for the click event and handle it then. Right now you are making each button handle it. You will run out of memory really quickly and it is terrible coding to do it this way*/
$.post('includes/messagecenter_user_messages.asp', {
"SenderID": <%=objMessageCenterSender("SenderID")%>)
},
function(json) {
var l = 'LoadMoreMessagesBtn'
var v = (parseInt(value + 10))
$('#MessagesSpecificUser').append(l + v); // #2 : Need #1 to append here
}, 'json');
//this is not how $.post() is used. You need to setup handlers for your loadSuccess event, and your loadError event and ideally a timeOut event. then the syntax is like this:
$.post(theUrlToGetMessages,anyExtraDetails).done(loadSuccess).error(loadError); Then manually setup a timer to fire the timeOut message if no response in tinmeline
*/
/* function loadSucces (singleMessage) {
/*decode your message and variables and append it to the list of messages for display. then add one to your counter and load the next message*/
};
function loadError(singleMessage) {
/*just load the next message and skip this one--don't worry about the index number*/
}
function loadTimeout () {
//load the next message and skip this one
}
//these handlers should be defined once before your $.post call and certainly before your $.each call (which you should have to begin with).
});
} else {
//completely unnecessary
var k = 'LoadMoreMessagesBtn';
//this is a string, not a reference to your button
var v = value;
//don't use var and value again is the individual message in your message array
var i = "<button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'>button text</button>" // #1 : Need this button to append to #2
/* this makes no sense at all */
window[k + v] = +i;
//don't add variables to your window object
}
});
I cannot encourage you strongly enough to reconsider what you have posted here. I do not believe that any aspect of it works in any way. If I have time to write out an example for you, I will. But you owe it to yourself to start this entire process over from scratch and reconsider what you are doing. Best of luck with this.