0

This is my code:

inserisciMarker();

function scorriAllaLista(param) {
    alert(param);
}

function inserisciMarker() {
    markerClick = function () {
        $('#example').attr("onclick", "scorriAllaLista('hello'); return false;");
    };

    markerClick();
}

clicking on link, Uncaught ReferenceError: scorriAllaLista is not defined

Why? How can I fix it?

8
  • 1
    Most likely it isn't defined globally. window.scorriAllaLista = scorriAllaLista Why are you setting an onclick attribute with javascript rather than binding an event? Commented Mar 20, 2013 at 14:11
  • 4
    You should not use attr() to add event handlers! Commented Mar 20, 2013 at 14:12
  • @both: I can't, for this unresolved problem: stackoverflow.com/questions/13033506/… Commented Mar 20, 2013 at 14:13
  • seems that google maps "remove" all handlers.. so I can't attach it! Commented Mar 20, 2013 at 14:14
  • It doesn't remove handlers, you're just miss-interpreting results. Events simply aren't bubbling because google maps stops them before they reach the body. (which was explained in your previous question) Bind the event using the google maps api. That's why they included that functionality after all. Commented Mar 20, 2013 at 14:25

2 Answers 2

10

That's not how you bind event handler using jQuery.

Replace

$('#example').attr("onclick", "scorriAllaLista('hello'); return false;");

with

$('#example').click(function(){
    scorriAllaLista('hello');
    return false;
});
Sign up to request clarification or add additional context in comments.

10 Comments

Correct answer, probably the simplest solution, !!/ninjad again :)
The return false is redundant in this case.
@AndreasGrech I don't see why. Returning false tells jQuery to both stop propagation of the event and to prevent default click behavior. Depending on OP's will, it may be useful.
@AndreasGrech: OP used return false in original code so the comment might be better on the original question directed at OP?
@markzzz your question isn't clear ? Are you trying to inject code in an iframe coming from another origin ?
|
2

If you realy must do it the way you do, you can add the method to the global window object, similar to this:

window.scorriAllaLista = function(param) {
    alert(param);
}

DEMO - add method to window


As you are already using return false you might get away with a href="#" as return false; already stops propagation as well as prevents default behaviour.

3 Comments

+1 because this solves OP's real problem (which is somewhat different from what was initially told). I tested a simple variation using a dynamic function (thus avoiding the param).
But why I need to add function to the dom? the function should be visible inside functions.
@markzzz: I'm not 100% sure but it definetly has to do with the fact the method is called from within an onclick attribute. I don't know the intrinsics but the error message you got indicated the execution expected scorriAllaLista to be accessable on a global level. As dystroy' answer shows, using .click() will work without adding your method to window but you are not able to do that as you stated in your 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.