0

I am trying to call a jquery function when clicking on a link, but it doesn't work, the function is not called at all...

Function:

<script>
    $(document).ready(function () {

        $('#notif').click(function () {
            var id = $(this).attr("data-id");
                        alert('test');
            $.ajax({
                type: "POST",
                url: "inc/showNotif.php",
                dataType: "html",
                data: {
                    id: id
                },
                success: function (result) {
                    $('.displayNotif').html(result);
                    console.log(result)
                }
            });

        });
    });
    </script>

Link:

<a href="#" id="notif" data-id="<?php echo $_SESSION['ID']; ?>" class="dropdown-toggle" data-toggle="dropdown">
                            <i class="icon-bell2"></i>
                            <span class="visible-xs-inline-block position-right">Activity</span>
                            <span class="status-mark border-orange-400"></span>
                        </a>

EDIT

I discovered that the code between tags doesn't execute at all, not even if I remove the onclick event.

EDIT 2

The issue is $(document).ready(function () the code works without it, but why?

SOLVED

Issue was some jquery errors preventing document.rady

18
  • Hello I think this can help you !! Commented Jul 4, 2017 at 15:27
  • just remove href="#" or stop event propagation Commented Jul 4, 2017 at 15:28
  • you could also replace your href="#" for href="javascript:void(0);" Commented Jul 4, 2017 at 15:28
  • @Nicolas I did what you've said and the jquery function is still not being called, I don't think the href is the issue because it used to work :/ something else must be but I can't see it Commented Jul 4, 2017 at 15:32
  • Question: are you adding your <a> after the page has loaded? In the doc ready, (before the click handler) add: alert($('#notif').length) - what value do you get? What about alert($("[id=notif]").length) Commented Jul 4, 2017 at 15:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.