How to add text to modal on button click












1















I have this modal



 <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="credit-scoring">
<label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
<input type="text" class="form-control" id="credit-scoring">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
</div>
</div>
</div>
</div>


and I would like to add text in <span id="bankAssessBondId"></span> when the following button is clicked



<button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
Bank Assess
</button>


as I have multiple buttons for this, I'm using an EventListener as follows



var buttons = document.getElementsByClassName('btn btn-success bank');
for(var i=0; i<buttons.length; i++){
buttons[i].on("click", function(){
$('#bankAssessBondId').append($(this).value)
})
}


but the 'click' button event is not detected. How can I fix this?










share|improve this question























  • It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

    – John M
    Nov 19 '18 at 11:47











  • yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

    – andrex_jux
    Nov 19 '18 at 11:49
















1















I have this modal



 <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="credit-scoring">
<label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
<input type="text" class="form-control" id="credit-scoring">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
</div>
</div>
</div>
</div>


and I would like to add text in <span id="bankAssessBondId"></span> when the following button is clicked



<button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
Bank Assess
</button>


as I have multiple buttons for this, I'm using an EventListener as follows



var buttons = document.getElementsByClassName('btn btn-success bank');
for(var i=0; i<buttons.length; i++){
buttons[i].on("click", function(){
$('#bankAssessBondId').append($(this).value)
})
}


but the 'click' button event is not detected. How can I fix this?










share|improve this question























  • It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

    – John M
    Nov 19 '18 at 11:47











  • yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

    – andrex_jux
    Nov 19 '18 at 11:49














1












1








1


0






I have this modal



 <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="credit-scoring">
<label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
<input type="text" class="form-control" id="credit-scoring">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
</div>
</div>
</div>
</div>


and I would like to add text in <span id="bankAssessBondId"></span> when the following button is clicked



<button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
Bank Assess
</button>


as I have multiple buttons for this, I'm using an EventListener as follows



var buttons = document.getElementsByClassName('btn btn-success bank');
for(var i=0; i<buttons.length; i++){
buttons[i].on("click", function(){
$('#bankAssessBondId').append($(this).value)
})
}


but the 'click' button event is not detected. How can I fix this?










share|improve this question














I have this modal



 <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form>
<div class="credit-scoring">
<label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
<input type="text" class="form-control" id="credit-scoring">
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
</div>
</div>
</div>
</div>


and I would like to add text in <span id="bankAssessBondId"></span> when the following button is clicked



<button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
Bank Assess
</button>


as I have multiple buttons for this, I'm using an EventListener as follows



var buttons = document.getElementsByClassName('btn btn-success bank');
for(var i=0; i<buttons.length; i++){
buttons[i].on("click", function(){
$('#bankAssessBondId').append($(this).value)
})
}


but the 'click' button event is not detected. How can I fix this?







javascript ajax twitter-bootstrap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 11:35









andrex_juxandrex_jux

154




154













  • It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

    – John M
    Nov 19 '18 at 11:47











  • yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

    – andrex_jux
    Nov 19 '18 at 11:49



















  • It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

    – John M
    Nov 19 '18 at 11:47











  • yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

    – andrex_jux
    Nov 19 '18 at 11:49

















It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

– John M
Nov 19 '18 at 11:47





It would be good to have some more details, such as how those buttons are created - for example, if they're dynamically generated or populated from an ajax request then that style of event handler won't work...

– John M
Nov 19 '18 at 11:47













yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

– andrex_jux
Nov 19 '18 at 11:49





yes, the buttons are created by an ajax request. I have a list of transactions each of which has one of those buttons.

– andrex_jux
Nov 19 '18 at 11:49












4 Answers
4






active

oldest

votes


















0














For elements that are dynamically generated you'll need to use the delegated events form of the JQuery on() function:



$('body').on('click', '.btn.btn-success.bank', function() {
$('#bankAssessBondId').append($(this).value)
}


I've used "body" in this case as the parent element, but it could be any element that's a parent of the buttons.






share|improve this answer































    0














    When selecting an element with multiple classes then the selectors must be specified without spaces like below.



    $('.btn.bank.btn-success')


    Also in this, case the modal should be opened with the script as the span value inside modal needs to be changed dynamically.



    $('#bankAssess').modal('toggle');





    $(function() {


    $('.btn.bank.btn-success').click(function() {

    var val = $(this).attr("value");

    console.log(val);

    $('#bankAssessBondId').append(val);
    $('#bankAssess').modal('toggle');

    });


    });

    #bankAssessBondId {
    color: red;
    }

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

    <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
    <div class="modal-dialog" role="document">
    <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
    <span aria-hidden="true">×</span>
    </button>
    </div>
    <div class="modal-body">
    <form>
    <div class="credit-scoring">
    <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
    <input type="text" class="form-control" id="credit-scoring">
    </div>
    </form>
    </div>
    <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
    <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
    </div>
    </div>
    </div>
    </div>

    <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
    Bank Assess
    </button>

    <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
    Bank Assess
    </button>








    share|improve this answer

































      0














      You can get the button by its class name using



      $(".bank").on('click', function () {
      alert('clicked');
      $('#bankAssessBondId').append($(this).attr('value'));
      });


      Also append the value of the button to the id="bankAssessBondId"



      <span id="bankAssessBondId"></span>
      <button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
      Bank Assess
      </button>


      Using a sample of your code



      Here is a working example.
      https://codepen.io/anon/pen/oQGJOm






      share|improve this answer































        0














        Your issue looks to be that you are trying to use the jQuery on with an element that was discovered not using jQuery. The way I see it you have two possible solutions depending on what you are trying to achieve.





        1. use the addEventListener



          var buttons = document.getElementsByClassName('btn btn-success bank');
          for(var i = 0; i < buttons.length; i++) {
          buttons[i].addEventListener("click", function(){
          $('#bankAssessBondId').append($(this).value)
          })
          }



        2. use jQuery to select the buttons and use the .on function to apply a click listener to all items returned



          var buttons = $(".btn.btn-success.bank");

          buttons.on("click", () => {
          //do stuff here
          });



        I hope this helps!






        share|improve this answer

























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373796%2fhow-to-add-text-to-modal-on-button-click%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          For elements that are dynamically generated you'll need to use the delegated events form of the JQuery on() function:



          $('body').on('click', '.btn.btn-success.bank', function() {
          $('#bankAssessBondId').append($(this).value)
          }


          I've used "body" in this case as the parent element, but it could be any element that's a parent of the buttons.






          share|improve this answer




























            0














            For elements that are dynamically generated you'll need to use the delegated events form of the JQuery on() function:



            $('body').on('click', '.btn.btn-success.bank', function() {
            $('#bankAssessBondId').append($(this).value)
            }


            I've used "body" in this case as the parent element, but it could be any element that's a parent of the buttons.






            share|improve this answer


























              0












              0








              0







              For elements that are dynamically generated you'll need to use the delegated events form of the JQuery on() function:



              $('body').on('click', '.btn.btn-success.bank', function() {
              $('#bankAssessBondId').append($(this).value)
              }


              I've used "body" in this case as the parent element, but it could be any element that's a parent of the buttons.






              share|improve this answer













              For elements that are dynamically generated you'll need to use the delegated events form of the JQuery on() function:



              $('body').on('click', '.btn.btn-success.bank', function() {
              $('#bankAssessBondId').append($(this).value)
              }


              I've used "body" in this case as the parent element, but it could be any element that's a parent of the buttons.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 19 '18 at 12:01









              John MJohn M

              1,20941120




              1,20941120

























                  0














                  When selecting an element with multiple classes then the selectors must be specified without spaces like below.



                  $('.btn.bank.btn-success')


                  Also in this, case the modal should be opened with the script as the span value inside modal needs to be changed dynamically.



                  $('#bankAssess').modal('toggle');





                  $(function() {


                  $('.btn.bank.btn-success').click(function() {

                  var val = $(this).attr("value");

                  console.log(val);

                  $('#bankAssessBondId').append(val);
                  $('#bankAssess').modal('toggle');

                  });


                  });

                  #bankAssessBondId {
                  color: red;
                  }

                  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                  <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                  <div class="modal-dialog" role="document">
                  <div class="modal-content">
                  <div class="modal-header">
                  <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">×</span>
                  </button>
                  </div>
                  <div class="modal-body">
                  <form>
                  <div class="credit-scoring">
                  <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                  <input type="text" class="form-control" id="credit-scoring">
                  </div>
                  </form>
                  </div>
                  <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                  </div>
                  </div>
                  </div>
                  </div>

                  <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                  Bank Assess
                  </button>

                  <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                  Bank Assess
                  </button>








                  share|improve this answer






























                    0














                    When selecting an element with multiple classes then the selectors must be specified without spaces like below.



                    $('.btn.bank.btn-success')


                    Also in this, case the modal should be opened with the script as the span value inside modal needs to be changed dynamically.



                    $('#bankAssess').modal('toggle');





                    $(function() {


                    $('.btn.bank.btn-success').click(function() {

                    var val = $(this).attr("value");

                    console.log(val);

                    $('#bankAssessBondId').append(val);
                    $('#bankAssess').modal('toggle');

                    });


                    });

                    #bankAssessBondId {
                    color: red;
                    }

                    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                    <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                    <div class="modal-content">
                    <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                    </button>
                    </div>
                    <div class="modal-body">
                    <form>
                    <div class="credit-scoring">
                    <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                    <input type="text" class="form-control" id="credit-scoring">
                    </div>
                    </form>
                    </div>
                    <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                    </div>
                    </div>
                    </div>
                    </div>

                    <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                    Bank Assess
                    </button>

                    <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                    Bank Assess
                    </button>








                    share|improve this answer




























                      0












                      0








                      0







                      When selecting an element with multiple classes then the selectors must be specified without spaces like below.



                      $('.btn.bank.btn-success')


                      Also in this, case the modal should be opened with the script as the span value inside modal needs to be changed dynamically.



                      $('#bankAssess').modal('toggle');





                      $(function() {


                      $('.btn.bank.btn-success').click(function() {

                      var val = $(this).attr("value");

                      console.log(val);

                      $('#bankAssessBondId').append(val);
                      $('#bankAssess').modal('toggle');

                      });


                      });

                      #bankAssessBondId {
                      color: red;
                      }

                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                      <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                      <div class="modal-content">
                      <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">×</span>
                      </button>
                      </div>
                      <div class="modal-body">
                      <form>
                      <div class="credit-scoring">
                      <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                      <input type="text" class="form-control" id="credit-scoring">
                      </div>
                      </form>
                      </div>
                      <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                      </div>
                      </div>
                      </div>
                      </div>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                      Bank Assess
                      </button>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                      Bank Assess
                      </button>








                      share|improve this answer















                      When selecting an element with multiple classes then the selectors must be specified without spaces like below.



                      $('.btn.bank.btn-success')


                      Also in this, case the modal should be opened with the script as the span value inside modal needs to be changed dynamically.



                      $('#bankAssess').modal('toggle');





                      $(function() {


                      $('.btn.bank.btn-success').click(function() {

                      var val = $(this).attr("value");

                      console.log(val);

                      $('#bankAssessBondId').append(val);
                      $('#bankAssess').modal('toggle');

                      });


                      });

                      #bankAssessBondId {
                      color: red;
                      }

                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                      <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                      <div class="modal-content">
                      <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">×</span>
                      </button>
                      </div>
                      <div class="modal-body">
                      <form>
                      <div class="credit-scoring">
                      <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                      <input type="text" class="form-control" id="credit-scoring">
                      </div>
                      </form>
                      </div>
                      <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                      </div>
                      </div>
                      </div>
                      </div>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                      Bank Assess
                      </button>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                      Bank Assess
                      </button>








                      $(function() {


                      $('.btn.bank.btn-success').click(function() {

                      var val = $(this).attr("value");

                      console.log(val);

                      $('#bankAssessBondId').append(val);
                      $('#bankAssess').modal('toggle');

                      });


                      });

                      #bankAssessBondId {
                      color: red;
                      }

                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                      <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                      <div class="modal-content">
                      <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">×</span>
                      </button>
                      </div>
                      <div class="modal-body">
                      <form>
                      <div class="credit-scoring">
                      <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                      <input type="text" class="form-control" id="credit-scoring">
                      </div>
                      </form>
                      </div>
                      <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                      </div>
                      </div>
                      </div>
                      </div>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                      Bank Assess
                      </button>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                      Bank Assess
                      </button>





                      $(function() {


                      $('.btn.bank.btn-success').click(function() {

                      var val = $(this).attr("value");

                      console.log(val);

                      $('#bankAssessBondId').append(val);
                      $('#bankAssess').modal('toggle');

                      });


                      });

                      #bankAssessBondId {
                      color: red;
                      }

                      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

                      <div class="modal fade" id="bankAssess" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabelBank" aria-hidden="true">
                      <div class="modal-dialog" role="document">
                      <div class="modal-content">
                      <div class="modal-header">
                      <h5 class="modal-title" id="exampleModalLabelBank">Bank Assess <span id="bankAssessBondId"></span></h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">×</span>
                      </button>
                      </div>
                      <div class="modal-body">
                      <form>
                      <div class="credit-scoring">
                      <label for="credit-scoring" class="form-control-label">Credit Scoring:</label>
                      <input type="text" class="form-control" id="credit-scoring">
                      </div>
                      </form>
                      </div>
                      <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                      <button type="submit" class="btn btn-primary" id="submit-form-Bank">Submit</button>
                      </div>
                      </div>
                      </div>
                      </div>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond2">
                      Bank Assess
                      </button>

                      <button class="btn btn-success bank" type="button" data-target="#bankAssess" value="bond3">
                      Bank Assess
                      </button>






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 19 '18 at 12:08

























                      answered Nov 19 '18 at 12:02









                      mbharanidharan88mbharanidharan88

                      4,04942455




                      4,04942455























                          0














                          You can get the button by its class name using



                          $(".bank").on('click', function () {
                          alert('clicked');
                          $('#bankAssessBondId').append($(this).attr('value'));
                          });


                          Also append the value of the button to the id="bankAssessBondId"



                          <span id="bankAssessBondId"></span>
                          <button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
                          Bank Assess
                          </button>


                          Using a sample of your code



                          Here is a working example.
                          https://codepen.io/anon/pen/oQGJOm






                          share|improve this answer




























                            0














                            You can get the button by its class name using



                            $(".bank").on('click', function () {
                            alert('clicked');
                            $('#bankAssessBondId').append($(this).attr('value'));
                            });


                            Also append the value of the button to the id="bankAssessBondId"



                            <span id="bankAssessBondId"></span>
                            <button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
                            Bank Assess
                            </button>


                            Using a sample of your code



                            Here is a working example.
                            https://codepen.io/anon/pen/oQGJOm






                            share|improve this answer


























                              0












                              0








                              0







                              You can get the button by its class name using



                              $(".bank").on('click', function () {
                              alert('clicked');
                              $('#bankAssessBondId').append($(this).attr('value'));
                              });


                              Also append the value of the button to the id="bankAssessBondId"



                              <span id="bankAssessBondId"></span>
                              <button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
                              Bank Assess
                              </button>


                              Using a sample of your code



                              Here is a working example.
                              https://codepen.io/anon/pen/oQGJOm






                              share|improve this answer













                              You can get the button by its class name using



                              $(".bank").on('click', function () {
                              alert('clicked');
                              $('#bankAssessBondId').append($(this).attr('value'));
                              });


                              Also append the value of the button to the id="bankAssessBondId"



                              <span id="bankAssessBondId"></span>
                              <button class="btn btn-success bank" type="button" data-toggle="modal" data-target="#bankAssess" value="bond3">
                              Bank Assess
                              </button>


                              Using a sample of your code



                              Here is a working example.
                              https://codepen.io/anon/pen/oQGJOm







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 19 '18 at 12:09









                              oOo--STAR--oOooOo--STAR--oOo

                              3431418




                              3431418























                                  0














                                  Your issue looks to be that you are trying to use the jQuery on with an element that was discovered not using jQuery. The way I see it you have two possible solutions depending on what you are trying to achieve.





                                  1. use the addEventListener



                                    var buttons = document.getElementsByClassName('btn btn-success bank');
                                    for(var i = 0; i < buttons.length; i++) {
                                    buttons[i].addEventListener("click", function(){
                                    $('#bankAssessBondId').append($(this).value)
                                    })
                                    }



                                  2. use jQuery to select the buttons and use the .on function to apply a click listener to all items returned



                                    var buttons = $(".btn.btn-success.bank");

                                    buttons.on("click", () => {
                                    //do stuff here
                                    });



                                  I hope this helps!






                                  share|improve this answer






























                                    0














                                    Your issue looks to be that you are trying to use the jQuery on with an element that was discovered not using jQuery. The way I see it you have two possible solutions depending on what you are trying to achieve.





                                    1. use the addEventListener



                                      var buttons = document.getElementsByClassName('btn btn-success bank');
                                      for(var i = 0; i < buttons.length; i++) {
                                      buttons[i].addEventListener("click", function(){
                                      $('#bankAssessBondId').append($(this).value)
                                      })
                                      }



                                    2. use jQuery to select the buttons and use the .on function to apply a click listener to all items returned



                                      var buttons = $(".btn.btn-success.bank");

                                      buttons.on("click", () => {
                                      //do stuff here
                                      });



                                    I hope this helps!






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      Your issue looks to be that you are trying to use the jQuery on with an element that was discovered not using jQuery. The way I see it you have two possible solutions depending on what you are trying to achieve.





                                      1. use the addEventListener



                                        var buttons = document.getElementsByClassName('btn btn-success bank');
                                        for(var i = 0; i < buttons.length; i++) {
                                        buttons[i].addEventListener("click", function(){
                                        $('#bankAssessBondId').append($(this).value)
                                        })
                                        }



                                      2. use jQuery to select the buttons and use the .on function to apply a click listener to all items returned



                                        var buttons = $(".btn.btn-success.bank");

                                        buttons.on("click", () => {
                                        //do stuff here
                                        });



                                      I hope this helps!






                                      share|improve this answer















                                      Your issue looks to be that you are trying to use the jQuery on with an element that was discovered not using jQuery. The way I see it you have two possible solutions depending on what you are trying to achieve.





                                      1. use the addEventListener



                                        var buttons = document.getElementsByClassName('btn btn-success bank');
                                        for(var i = 0; i < buttons.length; i++) {
                                        buttons[i].addEventListener("click", function(){
                                        $('#bankAssessBondId').append($(this).value)
                                        })
                                        }



                                      2. use jQuery to select the buttons and use the .on function to apply a click listener to all items returned



                                        var buttons = $(".btn.btn-success.bank");

                                        buttons.on("click", () => {
                                        //do stuff here
                                        });



                                      I hope this helps!







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 25 '18 at 21:42









                                      marc_s

                                      577k12911141259




                                      577k12911141259










                                      answered Nov 19 '18 at 11:50









                                      kriddy800kriddy800

                                      1347




                                      1347






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373796%2fhow-to-add-text-to-modal-on-button-click%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          這個網誌中的熱門文章

                                          Xamarin.form Move up view when keyboard appear

                                          Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                          Anylogic : not able to use stopDelay()