Jump to content

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);
      });
   }

 

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...
	});
});

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.