your code:
$("#div1").click(alert('kk'),function(){
});
when executed at startup calls alert() and passes the alert() return value (void) as the event data to be passed to the callback method, the actual callback function does nothing.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi All pls let me know why when I click on div1 it is not working As like callback function
div1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hide on Mouseover Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<div id="hideDiv" >
<br>
<div id="div1">fsdfsdfsdfs</div>
<!-- Content inside the div -->
<p>This is some content inside the div.</p>
</div>
<script>
$(document).ready(function() {
// Attach mouseenter event handler to the div
$("#hideDiv").on("mouseenter", function() {
// Hide the div with animation
$(this).hide("slow", function() {
console.log("The div is now hidden.");
});
});
$("#div1").click(alert('kk'),function(){
});
});
</script>
</body>
</html>
your code:
$("#div1").click(alert('kk'),function(){
});
when executed at startup calls alert() and passes the alert() return value (void) as the event data to be passed to the callback method, the actual callback function does nothing.
Hello @Siddangoud Bharamagoudar
The issue is with the way you’re attaching the click event handler to #div1
. The alert('kk')
is being executed immediately when the page loads, not when #div1
is clicked. This is because alert('kk')
is a function call, not a function definition.
Here’s how you can fix it:
<script>
$(document).ready(function() {
// Attach mouseenter event handler to the div
$("#hideDiv").on("mouseenter", function() {
// Hide the div with animation
$(this).hide("slow", function() {
console.log("The div is now hidden.");
});
});
// Correct way to attach click event handler
$("#div1").click(function(){
alert('kk');
});
});
</script>
In this corrected code, alert('kk')
is wrapped inside a function definition, which will be called when #div1
is clicked.
This should make your click event work as expected. If this solves your question, please tag this as answered, to help other community readers who may have similar questions.