Is there a better way to supply an onclick
function assignment to something echo
ed in php? I'm trying to open a modal div
once an <a>
link or <button>
is clicked.
<div id="modalBox" class="w3-modal">
<div class="w3-modal-content">
<div class="w3-container">
<span onclick="document.getElementById('modalBox').style.display='none'" class="w3-button w3-display-topright">×</span>
<p>Some text. Some text. Some text.</p>
<p>Some text. Some text. Some text.</p>
</div>
</div>
</div>
<div class="record-container">
<table class="table-record">
<tr>
<th>Title</th>
<th>Date Created</th>
<th>Control</th>
</tr>
<?php
$announcementDaoImpl = new AnnouncementDaoImpl($pdo);
$announcementList = $announcementDaoImpl->getAllAnnouncementByMostRecent();
foreach ($announcementList as $key => $value) {
$announcementTitle = $value->getTitle();
$dateCreated = $value->getDateAdded();
echo "<tr>";
echo "<td>" . $announcementTitle . "</td>";
echo "<td>" . $dateCreated . "</td>";
echo "<td><button onclick='showEditModal()'>" . "Edit" . "</button></td>";
echo "</tr>";
}
?>
</table>
</div>
This line,
echo "<td><a href='#' onclick='showEditModal()'>" . "Edit" . "</a></td>";
I assigned showEditModal()
as method so that once the Edit button is clicked, it will call on this method to display the modal box. However, I'm having problems with double quotes and single quotes because the showEditModal()
method expects an argument.
function showEditModal(modalDivId){
var modalBox = document.getElementById(modalDivId);
modalBox.style.display = "block";
}
I even tried this.
echo "<td><button onclick=\"showEditModal('modalBox')\">" . "Edit" . "</button></td>";
No luck. The modal box won't show. Even an alert()
won't show.
How can I supply the argument on echo
?
Thanks.