Dashboards & Visualizations

How to work with javascript in splunk

ektasiwani
Communicator

Hi,

I am working with simple xml and i want to hide some html line using javascript.
I have a simple java script file how to make it work on splunk.
I am not that much into javascript.
do we need to add this:

   require([    
    "splunkjs/ready!",    
    "splunkjs/mvc/simplexml/ready!"    
], function(

Simple javascript code is like this :

<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>

<script>
 $(document).ready(function() {

    $('.faq_question').click(function() {

        if ($(this).parent().is('.open')){
            $(this).closest('.faq').find('.faq_answer_container').animate({'height':'0'},500);
            $(this).closest('.faq').removeClass('open');

            }else{
                var newHeight =$(this).closest('.faq').find('.faq_answer').height() +'px';
                $(this).closest('.faq').find('.faq_answer_container').animate({'height':newHeight},500);
                $(this).closest('.faq').addClass('open');
            }

    });

});
</script>

Sorry if this question sounds silly.

0 Karma
1 Solution

alacercogitatus
SplunkTrust
SplunkTrust

You can't add javascript directly to a Simple XML Dashboard. Create a file in $APP_HOME/appserver/static, let's call it "dashboard.js". Place this code into it:

require( "jquery", function($) {

$(document).ready(function() {

 $('.faq_question').click(function() {

     if ($(this).parent().is('.open')){
         $(this).closest('.faq').find('.faq_answer_container').animate({'height':'0'},500);
         $(this).closest('.faq').removeClass('open');

         }else{
             var newHeight =$(this).closest('.faq').find('.faq_answer').height() +'px';
             $(this).closest('.faq').find('.faq_answer_container').animate({'height':newHeight},500);
             $(this).closest('.faq').addClass('open');
         }

 });

});

});

Once you have that in place with the correct permissions, restart Splunk web. This will pull the Javascript and work accordingly.

View solution in original post

alacercogitatus
SplunkTrust
SplunkTrust

You can't add javascript directly to a Simple XML Dashboard. Create a file in $APP_HOME/appserver/static, let's call it "dashboard.js". Place this code into it:

require( "jquery", function($) {

$(document).ready(function() {

 $('.faq_question').click(function() {

     if ($(this).parent().is('.open')){
         $(this).closest('.faq').find('.faq_answer_container').animate({'height':'0'},500);
         $(this).closest('.faq').removeClass('open');

         }else{
             var newHeight =$(this).closest('.faq').find('.faq_answer').height() +'px';
             $(this).closest('.faq').find('.faq_answer_container').animate({'height':newHeight},500);
             $(this).closest('.faq').addClass('open');
         }

 });

});

});

Once you have that in place with the correct permissions, restart Splunk web. This will pull the Javascript and work accordingly.

martin_mueller
SplunkTrust
SplunkTrust

Additionally, look at https://splunkbase.splunk.com/app/1603/ for many more examples adding little snippets of JS to SimpleXML dashboards.

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...