title: How can I add dynamic variable name into element in other scope - jQuery
body: <p>I need to write a dynamic variable name into a .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 append to but onlye the text i.e &quot;LoadMoreMessagesBtn20&quot;.</p>
  <p>Can anyone help me find a solution that works? :-)</p>
  <p>The below is part of a much larger script, but has been minified with the most needed parts as per the rules. The lines of interests I have marked as // #1 and // #2 in the below minified code:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const MyArray = json.MessageArrayCountTens
  const myArr = JSON.parse(MyArray);
  
  var LoadMoreMessagesBtn
  
  $.each(myArr, function(index, value) {
  
    if (value == 10) {
  
      $('#MessagesSpecificUser').append("&lt;button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'&gt;button text&lt;/button&gt;");
  
      $("#LoadMoreMessages_" + json.SenderID + "_" + value + "_btn").on("click", function() {
  
          $.post('includes/messagecenter_user_messages.asp', {
                "SenderID": &lt;%=objMessageCenterSender("SenderID")%&gt;)
            },
            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 = "&lt;button id='LoadMoreMessages_" + json.SenderID + "_" + value + "_btn' type='button'&gt;button text&lt;/button&gt;" // #1 : Need this button to append to #2
  
    window[k + v] = +i;
  
  }
  
  });</code></pre>
  </div>
  </div>
  </p>