0

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!

11
  • How many elements have an id of NotesAccessor? Also, do you get any errors in your JS console? Commented Feb 9, 2013 at 22:33
  • None just that one, no. I just debug it in Chrome. Commented Feb 9, 2013 at 22:35
  • 2
    Are you wrapping your jQuery in a document.ready call? Commented Feb 9, 2013 at 22:35
  • 2
    Note that all of the (non-PHP) code that you show is JavaScript. There is no magic trick to "calling JavaScript in Jquery" because all of jQuery is JavaScript. Commented Feb 9, 2013 at 22:37
  • 1
    @j08691 you did....wrapping it in the document.ready call. Commented Feb 9, 2013 at 22:40

2 Answers 2

4

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();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

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.