Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function not calling within an onclick event

I want to add some HTML to the end of every youtube link to open up the player in a litebox. This is my code so far:

$(document).ready(function() {   var valid_url = new RegExp('youtube\.com\/.*v=([a-zA-Z0-9_-]+)');   var image_data = 'base64 encoded image';    init();    function init() {     $('a').each(function() {       if (valid_url.test($(this).attr('href'))) {         $(this).after( ' <img src="' + image_data + '" onclick="open_litebox(\'hi\');" />' );       }     });   }    function open_litebox(param) {     alert(param);   } }); 

It works to the point where it injects some HTML after the youtube link, like so:

<img src="base 64 data" onclick="open_litebox('hi')"> 

But when I click this the open_litebox() function doesn't get called. Looking in the error console I can see an error that says open_litebox is not defined, but I have defined it.

I'm pretty clueless as to what's going wrong here, could someone lend me a hand?

Thanks.

like image 771
Wen Avatar asked Nov 22 '10 05:11

Wen


People also ask

What's the difference between onclick ={ () => function ()} and onclick function?

The difference is the first one has the parentheses and the second one doesn't.

Is not a function at onclick?

onclick is not a function" error occurs, because there is no onclick() function in jQuery. To solve the error, use the click function instead, e.g. $('#btn'). click(function () {} . Copied!

Can you pass two functions to Onclick?

Yes, you can call two JS Function on one onClick. Use semicolon (';') between both the functions.

Does Onclick work on any element?

The onclick attribute is part of the Event Attributes, and can be used on any HTML elements.


2 Answers

This is a common problem when you first start working with jQuery. The problem here is that you have defined the function within the jQuery scope, which means that it is not accessible by just calling it like a normal function. A solution to your problem is to move your function definition outside the anonymous ready function that you written, like so:

$(document).ready(function() {      // do your stuff here  });  // define your functions here  function my_func() {  } 

Oh and I would suggest doing the same for your variables that you have defined. Move them outside your ready function as well, because you will have the same problems as you did with your functions.

like image 66
Simon H Avatar answered Sep 18 '22 16:09

Simon H


Your open_litebox function is in the scope of another function, so it is not globally visible. Instead of what you are doing, try using .click():

  • http://api.jquery.com/click/

Something along these lines:

$(this).after( ' <img src="' + image_data + '" id='abc' />' ); $('#abc').click(open_litebox); 

You would have to generate separate IDs for each of the links using this way, so another way is to make each of <img> tags have a known class attribute and then select it using $('.abc') (if the class is abc) instead of $('#abc') and do it in one step (i.e. as a separate call after your $('a').each).

like image 41
icyrock.com Avatar answered Sep 16 '22 16:09

icyrock.com