Joomla 3.8 and Jquery - Does not work

Asked

Viewed 105 times

0

Hello! I searched everything that was singing and couldn’t find a step-by-step how to use Jquery in Joomla 3.8. I am creating a module and I would like to use Jquery.

I made it here:

1 - download jquery-3.3.1.js and put it in the folder: /modules/mod_mymodule/js

  1. Reading the Joomla documentation, I added the following lines to my modules/mod_mymodule/mod_mymodule.php file:

    JHtml::script(JURI::base() . 'js/jquery-3.3.1.js');
    Jhtml::_('jquery.framework');
    Jhtml::_('jquery.ui');
    
  2. At the bottom of my default.php file I added this:

    <?php
    $document = JFactory::getDocument();
    $document->addScriptDeclaration('
        window.event("domready", function() {
            alert("An inline JavaScript Declaration");
        });
    ');
    ?>
    

It should show an alert window when the page loads but does not work. What I am doing wrong?

  • Ten try to change window.event("domready", function() {&#xA; alert("An inline JavaScript Declaration");&#xA; }); for $(function() {&#xA; alert("An inline JavaScript Declaration");&#xA; });

  • But nothing happens doing it this way.

  • Take a look at that answer, it’s for an older version of Joomla, but I believe it might work for you! You have two options how to do this https://answall.com/questions/351739/inserindo-javascript-na-header-do-joomla-2-5-8/351849#351849

1 answer

-1

To avoid conflict with Joomla libraries you should use JQuery with a different format. Instead of using the $ to fetch the ID, CLASS, etc, you must use the name jQuery:

var variavel = jQuery("#id_do_elemento_html").val();

On this page (Docs.joomla.org/J3.x:Developing_an_mvc_component/Adding_a_map) you will find an example of how to add a map, here the author makes use of Jquery the way I showed above. This is an official Joomla page

I do it like this and it always works.

  • This may be the solution, but it would be interesting for you to explain to the author of the question, with official documentation, why this is the output: https://docs.joomla.org/J3.x:Javascript_frameworks#jQuery_JavaScript_Framework

  • On this page (https://docs.joomla.org/J3.x:Developing_an_mvc_component/Adding_a_map) you will find an example of how to add a map, here the author makes use of Jquery the way I showed above. This is an official Joomla page.

  • Below the answer is three buttons compartilhar editar sinalizar. Click on editar and a fancy answer. As soon as you edit it, it goes to the main page getting more visibility and gaining the possibility to get some votes up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.