Splunk Search
Highlighted

How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

Hi,

I have a dashboard in html code with one search query which provides the result in Single Numeric Value.

Is there any way that I can take the result of my search in some variable?
I want to display it in a JavaScript alert popup i.e. alert("my search result is: " + search_result_var).
Which variable in html code of dashboard stores the search result.
Please Help..!!!

0 Karma
Highlighted

Re: How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

Motivator

Have you tried assigning the value to a input token and call it ? might work. I had a same single value panel...i created a token and assigned the single value and then called the token like

value is : $single_value$

and it displays just fine.

Hope this helps!
Thanks,
Raghav

0 Karma
Highlighted

Re: How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

New Member

Thank you so much!! It was really helpful 🙂

0 Karma
Highlighted

Re: How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

Splunk Employee
Splunk Employee

One way to achieve this is put a listener on your search and then assign the value to a token. For instance if my single panel was using search1 and the token I wanted to set was "user".

 var tokens = mvc.Components.getInstance("default");
 var singleresult=splunkjs.mvc.Components.get("search1");
     singleresult.data("results").on("data", function(results) { 
             var username = results._data['rows'][0][0];
             tokens.set("user",username);
      });

You could then use that token or variable anywhere in your HTML. You can use it in a search as any other token by calling it with $user$ or in your script by the variable alert("my search result is: " + username). If you're not using the alert inside the listener, you can retrieve the token value and set it as a variable anywhere else by -

 var desiredvariable=tokens.get("user");
Highlighted

Re: How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

Explorer

@Flynt[Splunk]

Thanks for your help! 🙂

0 Karma
Highlighted

Re: How to store a single value search result in some variable in a Splunk HTML page to display in a javascript alert popup?

New Member

Hi i am new to splunk, can you please give me a sample code which i can use?

0 Karma