I’m spinning through a bunch of elements, and want to add a link which will run a short snippet of JavaScript. I’m doing this will a content script in a Chrome extension. The function do_something is defined in the same file as my_func, and is part of the Chrome extension.
my_func looks like this:
function my_func() {
$(".matching_class").each(function() {
$(this).html('<a href="javascript:do_something();">foo</a>');
});
}
Problem is, when I click on my shiny links, I get this:
Uncaught ReferenceError:
do_somethingis not defined
How can I reference do_something from my_func?
It would probably be better practice to bind your function to the click event of the hyperlink rather than injecting a call to javascript: my_func();
Something along these lines (untested):
or with an anonymous function: