0

I'm trying to add a little funcionality to one of my Joomla article. The functionality is quite simple, when you click on a string, a html shows. I had made an standalone HTML and it's working.

Now I'm trying to include the code on my Joomla article, but its not working. This is what I added to the source code of the article.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
    jQuery(function(){
        jQuery('.showItem').click(function(){
                    jQuery('.targetDiv').hide();
                    jQuery('#div'+$(this).attr('target')).show();
            });
    }); 
</script>

<!-- BUTTONS  -->
<div class="buttons">
    <a class="showItem" target="1">First string</a><br>
    <a class="showItem" target="2">Second string</a><br>
</div>

<!-- Shows HTML page when you click in 'First string' -->
<div id="div1" class="targetDiv" style="display: none">
    <div id="m1" style="margin: 0 auto; width:100%; height:400px;">
        <object type="text/html" data="http://show_a_html_page.html"
                style="width:100%; height:100%; margin:1%;">
        </object>
    </div>
</div>

<!-- Shows HTML page when you click in 'Second string' -->
<div id="div2" class="targetDiv" style="display: none">
    <div id="m2" style="margin: 0 auto; width:100%; height:400px;">
        <object type="text/html" data="http://show_a_html_page.html"
                style="width:100%; height:100%; margin:1%;">
        </object>
    </div>
</div>

It's not the problem of the editor (aka it's not removing the <script> tags).

Do I've to change something in my script in order to make this work?

In case this can be interesting, after adding the code as Source Code in my post, this is what shows up the TinyMCE. It adds some <p> to the code as well as a / <![CDATA[. Is this right?

<p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>// <![CDATA[
jQuery(function(){
        jQuery('.showItem').click(function(){
                    jQuery('.targetDiv').hide();
                    jQuery('#div'+$(this).attr('target')).show();
            });
    });
// ]]></script>
</p>
Avión
  • 7,963
  • 11
  • 64
  • 105

1 Answers1

0

As @BhojendraNepal stated, it was a conflict problem. I fixed it by adding var $ = jQuery.noConflict(); to the function.

<script>
    jQuery(function(){
        var $ = jQuery.noConflict();
        jQuery('.showItem').click(function(){
                    jQuery('.targetDiv').hide();
                    jQuery('#div'+$(this).attr('target')).show();
            });
    }); 
</script>

EDIT: The marked as duplicate question doesn't provide an answer to the question. As that answer stated, I added <script>jQuery.noConflict(true);//remove jquery</script> before loading my jquery but it didn't worked:

<script>jQuery.noConflict(true);//remove jquery</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
    jQuery(function(){
        jQuery('.showItem').click(function(){
                    jQuery('.targetDiv').hide();
                    jQuery('#div'+$(this).attr('target')).show();
            });
    }); 
</script>

Just adding var $ = jQuery.noConflict(); inside the function worked perfectly.

Avión
  • 7,963
  • 11
  • 64
  • 105