Splunk Search

button "click" event isn't working

puet
Explorer

So I'm trying to change a token when i click a button.

Tried it like this:

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function ( $,mvc,TableView) {
    var tokens = mvc.Components.get('default');
    var sub_tok = mvc.Components.get("submitted");

    $(document).on("click","#testbtn",function(){
        tokens.set("btnClick", "Click");
        sub_tok.set("btnClick", "Click");
    });
});

And like this:

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function ( $,mvc,TableView) {
    var tokens = mvc.Components.get('default');
    var sub_tok = mvc.Components.get("submitted");

    $("#testbtn").on("click",function(){
        tokens.set("btnClick", "Click");
        sub_tok.set("btnClick", "Click");
    });
});

But its not working. When i try the code in jsFiddle it works as intended. (leaving out the splunk stuff)
It's just a jQuery click event, so I don't know what I'm doing wrong.

Further more when I try the whole think with a slider, it works.

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/tableview',
    'splunkjs/mvc/simplexml/ready!'
], function ( $,mvc,TableView) {
    var tokens = mvc.Components.get('default');
    var sub_tok = mvc.Components.get("submitted");

   $("#slider-range").on("input change", function () {
        tokens.set("slider_value", $(this).val());
        sub_tok.set("slider_value", $(this).val());
    });
});

 Here ist the xml code:

<dashboard script="testscript.js" theme="dark">
  <label></label>
  <fieldset submitButton="false">
    <html>
            <label>Slider</label>
            <input type="range" id="slider_input" value="10" min="0" max="20" step="1"/>
            <button id="testbtn">ghfhfhfhfh</button>
        </html>
  </fieldset>
  <row>
    <panel>
      <title>$slider_value$____$btnClick$</title>
      <table>
        <search>
          <query>|makeresults | eval slider_value=$slider_value$</query>
          <earliest>-1h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="refresh.display">progressbar</option>
      </table>
    </panel>
  </row>
</dashboard>

I there is a simple solution to this and thanks in advance.

Labels (1)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@puet 

 Can you please try this?

require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc) {
    var tokens = mvc.Components.get("default");
    var submitted_tokens = mvc.Components.get("submitted");
    $(document).on("click", "#testbtn", function() {
        tokens.set("sliderval", $('#slider_input').val());
        tokens.set("slider_value", $('#slider_input').val());
        tokens.set("btnClick", "Click");
        submitted_tokens.set(tokens.toJSON());
    });
});

 

Thanks
KV
▄︻̷̿┻̿═━一   😉

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

@puet 

 Can you please try this?

require([
    'underscore',
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function(_, $, mvc) {
    var tokens = mvc.Components.get("default");
    var submitted_tokens = mvc.Components.get("submitted");
    $(document).on("click", "#testbtn", function() {
        tokens.set("sliderval", $('#slider_input').val());
        tokens.set("slider_value", $('#slider_input').val());
        tokens.set("btnClick", "Click");
        submitted_tokens.set(tokens.toJSON());
    });
});

 

Thanks
KV
▄︻̷̿┻̿═━一   😉

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

puet
Explorer

Thanks it works now. 
Do i guess you always need "underscore" for it to work. 
I thought I already tried this but maybe I messed up the order or sth.

Thanks again!

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@puet 

underscore is just extra you can remove it. 

like 

require([
    'jquery',
    'splunkjs/mvc',
    'splunkjs/mvc/simplexml/ready!'
], function($, mvc) {
    var tokens = mvc.Components.get("default");
    var submitted_tokens = mvc.Components.get("submitted");
    $(document).on("click", "#testbtn", function() {
        tokens.set("sliderval", $('#slider_input').val());
        tokens.set("slider_value", $('#slider_input').val());
        tokens.set("btnClick", "Click");
        submitted_tokens.set(tokens.toJSON());
    });
});

It only need when you use inn your JS code. 🙂

 Thanks
KV
▄︻̷̿┻̿═━一   😉

If any of my reply helps you to solve the problem Or gain knowledge, an upvote would be appreciated.

 

0 Karma

puet
Explorer

I had exactly the same code without it working. But i guess now that it works i shouldn't complain.

0 Karma
Get Updates on the Splunk Community!

Introducing the Splunk Community Dashboard Challenge!

Welcome to Splunk Community Dashboard Challenge! This is your chance to showcase your skills in creating ...

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...