I have a jsp page that contains a username and a button, When I click on the button I want to pass the username to the modal then in the click on the modal button I send the username to the back-end (struts2 action). (Scenario illustrated in the below picture)
I tried to pass username with onClick :
<%-- Page.jsp --%>
<button onclick="document.forms[0].username.value='BOB'; toggle="modal" data-target="#myModal">
<fmt:message key='Delete user'/>
</button>
<jsp:include page="deleteUserModal.jsp"/>
On the modal I want to read unsername value :
<%-- Modal.jsp --%>
<button onclick="document.forms[0].username.value='???';document.forms[0].action='deleteMethod';document.forms[0].submit();">
<fmt:message key='delete'/>
</button>
Any idea please,
You trying to access elements from the another window. To customize the modal content you should use HTML data-* attributes. See how to use verying modal content with bootstrap.
<button onclick="document.forms[0].username.value='BOB'; toggle="modal" data-target="#myModal" data-username='BOB'>
<fmt:message key='Delete user'/>
</button>
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var username = button.data('username')
var modal = $(this)
modal.find('.modal-body button').click(function(){
alert(username);
})
})