title: How to display modal on button click
body: <p>I need to open a modal using button's <code>onclick</code>, but it is not succeeding. Are there any errors in my code?</p>
  
  <p><strong>Add to Bag button</strong></p>
  
  <pre><code>&lt;button class="btn btn-default" id="addtobag" onclick="addTobag();"&gt;Add to Bag&lt;/button&gt;&lt;br&gt;
  </code></pre>
  
  <p><strong>Modal</strong></p>
  
  <pre><code>&lt;div class="modal fade" id="addtobagmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true"&gt;
      &lt;div class="modal-dialog modal-dialog-centered modal-lg" role="document"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
                  //mycode
              &lt;/div&gt;
              &lt;div class="modal-body"&gt;
                  //mycode
              &lt;/div&gt;
              &lt;div class="modal-footer"&gt;
                  //mycode
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Function</strong></p>
  
  <pre><code>function addTobag() {
      var request = new XMLHttpRequest();
      request.onreadystatechange = function () {
          if (request.readyState === 4 &amp;&amp; request.status === 200) {
              if (request.responseText == "OK") {
                  $('#addtobagmodal').modal('show');
              } else {
                  alert('error');
              }
          }
      };
      request.open("GET", "myBag", true);
      request.send();
  }
  </code></pre>