Why Callback is not working

Siddangoud Bharamagoudar 61 Reputation points
2024-02-12T17:22:38.34+00:00

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>

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,645 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
9,599 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
872 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,276 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,356 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2024-02-12T20:42:48.05+00:00

    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.

    0 comments No comments

  2. Pinaki Ghatak 2,400 Reputation points Microsoft Employee
    2024-02-13T18:34:59.7966667+00:00

    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.