<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
  <title>Posts tagged with “Jquery” on Mark van Lent’s weblog</title>
  <updated>2011-11-07T21:52:00+00:00</updated>
  <link rel="self" type="application/atom+xml" href="https://markvanlent.dev/tags/jquery/index.xml" hreflang="en"/>
  <id>tag:markvanlent.dev,2010-04-02:/tags/jquery/index.xml</id>
  <link rel="alternate" type="text/html" href="https://markvanlent.dev/tags/jquery/" hreflang="en"/>
  <author>
      <name>Mark van Lent</name>
      <uri>https://markvanlent.dev/about/</uri>
    </author>
  <rights>Copyright (c) Mark van Lent, Creative Commons Attribution 4.0 International License.</rights>
  <icon>https://markvanlent.dev/favicon.ico</icon>
  <entry>
    <title type="html"><![CDATA[Using a jQuery plugin in Django admin and getting a “$().foo is not a function” or “Object ... has no method foo” error?]]></title>
    <link rel="alternate" href="https://markvanlent.dev/2011/11/07/jquery-plugin-django-admin-foo-not-function-error-or-object-has-no-method-foo/" type="text/html" />
    <id>https://markvanlent.dev/2011/11/07/jquery-plugin-django-admin-foo-not-function-error-or-object-has-no-method-foo/</id>
    <author>
      <name>map[name:Mark van Lent uri:https://markvanlent.dev/about/]</name>
    </author>
    <category term="development" />
    <category term="django" />
    <category term="jquery" />
    
    <updated>2021-07-23T07:42:12Z</updated>
    <published>2011-11-07T21:52:00Z</published>
    <content type="html"><![CDATA[<p>Are you using a jQuery plugin, for instance <a href="https://jqueryui.com/">jQuery UI</a>,
to spice up the Django admin site? Then you might get either an error like &ldquo;<code>foo is not a function</code>&rdquo; (<a href="https://getfirebug.com/">Firebug</a>) or &ldquo;<code>Object ... has no method foo</code>&rdquo; (<a href="https://developer.chrome.com/docs/devtools/">Chrome Developer Tools</a>).
Are you confused because <code>foo</code> should be defined in the plugin? Don&rsquo;t worry, the
solution is simple.</p>
<p>Actually, the reason <em>and</em> solution are in the
<a href="https://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions">Django admin site documentation</a>
if you know where to look. The reason is this:</p>
<blockquote>
<p>Django&rsquo;s jQuery is namespaced as <code>django.jQuery</code></p></blockquote>
<p>This prevents collisions with other scripts or libraries. It also
prevents you from blindly using code you find on the internet. ;-)</p>
<p>Possible solutions are:</p>
<ol>
<li>Include your own copy of jQuery which does &lsquo;pollute&rsquo; the global namespace.</li>
<li>Make sure your plugin/code uses <code>django.jQuery</code> instead of just <code>$</code> or <code>jQuery</code>.</li>
<li>Create a <code>jQuery</code> (or <code>$</code>) variable yourself.</li>
</ol>
<p>As the documentation says, the benefit of the first option is that you
can use a different (newer) version of jQuery, if you want or need
to.</p>
<p>The second option might mean that you&rsquo;ll have to download the
development version of the plugin, change the code, and minify it
yourself.</p>
<p>I found the third option as
<a href="https://stackoverflow.com/a/7250616/122661">an answer</a>
to a question about the
<a href="https://stackoverflow.com/q/7188563/122661">jQuery namespace in Django admin</a>
on Stack Overflow.</p>
<p>For what it&rsquo;s worth, I chose the second option and changed the custom
jQuery UI code I had downloaded. Your use case might benefit from
another solution&hellip;</p>]]></content>
  </entry>
</feed>
