Reputation: 49384
I have a php function:
function myfunc() {
//then I have a div...
echo '<div class="overlay">';
echo "<button onclick=\"$('.overlay').hide();\">Close</button>";
echo '</div>';
}
My problem is that when I click on the close button the div is not hiding.
What I'm I doing wrong here?
Upvotes: 1
Views: 760
Reputation: 1531
Try this code:
function myfunc() {
//then I have a div...
echo '<div class="overlay" id="overlay" >';
echo "<button onclick=\"hide()\">Close</button>";
echo '</div>';
}
//using the javascript code:
function hide()
{
document.getElementById("overlay").style.display="none";
}
Upvotes: 2
Reputation: 11
try:
$('.overlay button').live('click',function(
$('.overlay').css({'display': 'none'});
));
Upvotes: 0
Reputation: 163
try:
<button onclick="this.parentNode.style.display = 'none'; return false;">Close</button>
Upvotes: 2
Reputation: 123377
Avoid to hardcode javascript handlers and inline events inside the output of php code: do instead
echo '<div class="overlay">';
echo "<button>Close</button>";
echo '</div>';
and previously insert in your page this code that detects a click on your button using event delegation
<script>
$(document).on('click', '.overlay button', function() {
$(this).parent().hide()
});
</script>
Upvotes: 5