Search code examples
javascripthtmliosuiwebviewdom-events

UIWebView Javascript onclick event not sending event object to function


I am dynamically inserting HTML using simple NSStrings and then loading that into a UIWebview, I am doing this:

[NSString stringWithFormat:@"<div onclick=\"MyClickEvent(e);\"....some more text here">

and have a function defined like this:

function MyClickEvent(e)
{
    //Some other code here
    e.stopPropagation();
    return false;
}

The function is never getting called, because of the way the parameter is being passed as e, if I use quotations like this:

[NSString stringWithFormat:@"<div onclick=\"MyClickEvent('e');\"....some more text here">

Then the function will be called but e is just a simple string as expected, how can I correctly pass the event object?

The HTML at the end simply looks like this:

<div onclick="MyClickEvent(e);">

which I believe is how it should be, but somehow the event object is not being sent, what am I doing wrong?.


Solution

  • I got it to work, apparently what you need to do is this:

    [NSString stringWithFormat:@"<div onclick=\"MyClickEvent(event);\"....some more text here">
    

    So simply naming the parameter event, does the trick.