Jump to content
Sign in to follow this  
fatkatie

Can a javascript function called via onclick use jquery?

Recommended Posts

The onclick event is set to call a javascript function.  I need to use jQuery within that javascript function.  Is that possible?

Thank you.

   function updateStock(pn) {
      alert("here is the pn: " + pn);
      jQuery(document).ready(function() {
         alert("I want to see pn here: " + pn);
      });
   }

 

Share this post


Link to post
Share on other sites

Yes. Depending on how everything is set up and when and where the function is defined you may have to pass jQuery to the function, but it's easily doable. For instance (and please note it's late and this hasn't been tested so ... you know, look up the functions but the idea is solid) this:

$('#mycontrol').on('click', function(e){
	e.preventDefault();
	$.post({
		// ....etc etc...
	});
});

 

Share this post


Link to post
Share on other sites

I had no idea!  Scope, closure, hoisting ...  I really don't know this stuff. 

I did get the function parameter pn into the jQuery part by simply assigning it as in (ff65):

 

function updateStock(pn) {
      alert("here is the pn: " + pn);
      var mypn = pn;
      jQuery(document).ready(function() {
         alert("I want to see pn here: " + mypn);
      });
   }

 

Lots of reading and re-reading to do.

 

Thanks

Share this post


Link to post
Share on other sites

You shouldn't even need to assign it to a new mypn variable like that.  your original code where you just used pn directly should work just fine.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.