WordPress jQuery working but not the alert function

I am working on a WordPress plugin and I’ve included jQuery. For some very strange reason the alert function is not being shown. See code below:

$("#my_button").click(function() {
      alert('before hide/show');  
      $('#divA').hide();
      $('#divB').show();
      alert('after hide/show'); 
});

In the code above divA is actually hidden and divB is shown, so it means jQuery is working, but strangely enough none of the alert are being shown.

Read More

I am running the code from the plugin options page in the backend.

Why is this happening?

Related posts

Leave a Reply

5 comments

  1. The problem was Firefox Cache. It was the last thing I expected it to be but after doing an Alt F5 it started working.

    Not sure, if it’s WordPress issue or just the FF browser but this sorted it for me.

    Can’t believe it was the most simple thing :o/ Lesson learned… If the code doesn’t work and it’s not making sense, press ALT F5 First.

  2. Your Code is working fine.
    you can see in FIDDLE.

    check your other code. May your another function or something else is preventing to show alert?

    Or put your code in ONLOAD on form.

    $(function() {
    });
    
  3. Have you called any jQuery file

    Try by adding following before your code

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    
  4. The alert function is override by some other code. That ‘s the reason why it doesn’t show the alert box.

    You can look at this example then you can understand what I mean

    function alert(param)
    {
    // do nothing
    }
    
    alert("Call message"); // this will not show up anything because the system function is override
    
  5. I had same problem. It was not doing a simple alert. The way I solved was just including jQuery.(document).ready after wp_enqueue_script like this:

    <?php
    add_action( 'wp_enqueue_script', 'load_jquery' );
    function load_jquery() {
      wp_enqueue_script( 'jquery' );
    }
    ?>
    
    //call jQuery
    jQuery.(document).ready(function(){
      alert('test');
    });