David Biga
David Biga

Reputation: 2801

Issue with calling JavaScript in Jquery

I am trying to call a function that is in javascript inside Jquery once a link is clicked. When I try and do this nothing happens.

Code:

function run(){
    var url = '/pcg/popups/grabnotes.php';
    showUrlInDialog(url);
}

$("#NotesAccessor").click(function () {
      run();
    });

PHP:

..
echo "<a href='#' id='NotesAccessor'>Click to access</a>";
..

Not to sure why, I check my calling in the debug and nothing gets called. From the run function it goes to a Jquery UI dialog that will open up. It just does not get to that point.

If you could give me a hand I would appreciate it!

Upvotes: 0

Views: 58

Answers (2)

Hector Romero
Hector Romero

Reputation: 262

Your problem could be because NotesAccessor is an anchor element. You can make NotesAccessor to be a div and with css you make look just like an anchor.

In the other hand you can call to a JavaScript function from an anchor directly something like:

<a href:'javascript:run()' >Click to access</a>

Upvotes: 0

marty
marty

Reputation: 4015

Probably your DOM isn't loaded yet when you define the click event for $("#NotesAccessor"). Wrap your instruction in a $(document).ready handler:

$(document).ready(function() {
    $("#NotesAccessor").click(function () {
      run();
    });
});

Upvotes: 4

Related Questions