I am new to this, so this is probably not the best solution, but it is a solution.
$('#project').on('change', function() { var p_id = $(this).val(); var p_name = $(this).find(':selected').text(); $('#myModal').on('show.bs.modal', function () { $(this).find('#p_name').text(p_name); $(this).find('#p_id').val(p_id); }); $('#myModal').modal('show'); });
You need to add the class or identifier to the hidden field so that it can be identified. Above, I gave it the identifier p_id.
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel"></h4> </div> <div class="modal-body modal-mt-hgt"> <form action="" method="post"> <p> Are you sure to comment on <b id="p_name">...PROJECT NAME HERE</b> </p> Enter Your Comment : <textarea rows="3"></textarea> <input type="hidden" id="p_id" name="country" value=""> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Add Task</button> </div> <?= form_close(); ?> </div> </div> </div>
I think all of this is self-explanatory, but if not, we add a check to see when the project drop-down list changes, then we assign the value and text of the selected item using the $ (this) variable to indicate that it is in # dropdown identifier project.
Then we will add a listener to see when the modal is shown, from there we can manipulate the modal as we want. In this example, we set the text p_name and set the value to p_id.
source share