0

I'm working with Wordpress and jQuery, where I can only use the "$" symbol by passing it as a parameter into functions which are defined with "jQuery".

But I don't know how to pass in the symbol into a named function.

This didn't work:

(function sendPostRequest($){
    $("<input>").attr({
        type: "hidden",
        name: "merchant",
        value: 90198778
    }).appendTo('#signup_form');
})(jQuery);

How can I make it work?

Galivan
  • 4,842
  • 9
  • 44
  • 76
  • This should work if jQuery is defined. Now in which context this snippet is called? – A. Wolff Oct 29 '15 at 09:27
  • IMHO, it is quite unclear what you are asking: `But I don't know how to pass in the symbol into a named function`? – A. Wolff Oct 29 '15 at 09:30
  • I like to use `jQuery(document).ready(function($) { "use strict";});` and put all my code inside. – dingo_d Oct 29 '15 at 09:41
  • @A.Wolff Maybe I didn't ask it in the semantically perfect way, but I wanted to be able to use the $ symbol inside a named function (not anonymous), and not necessarily a "document ready" function. – Galivan Oct 29 '15 at 09:50
  • Your best bet imho would be just to past `jQuery` as function parameter (as your IIFE is doing). And because you said `This didn't work` regarding posted snippet, i was quite confused – A. Wolff Oct 29 '15 at 09:56

1 Answers1

3

The simplest way is

function sendPostRequest(){
    var $ = jQuery;
    ....
JJJ
  • 32,902
  • 20
  • 89
  • 102