Splunk Search

Set token on submit button click?

nick405060
Motivator

Hi guys.

Can someone please post working js code for a button that toggles a token from "true" to "false" and back. Have attempted a ton of different js scripts but no dice.

One example that I have tried is below. The file loads and I bump etc.

 require([
     "splunkjs/mvc",
     "splunkjs/mvc/simplexml/ready!"
 ], function(
             mvc
             ) {
         var defaultTokenModel = mvc.Components.get("default");
         $("#btn-submit").on("click", function (){
             defaultTokenModel.set("clicked","true");
         });
 });

console.log("hello")
//# sourceURL=submitToken.js
Tags (1)
1 Solution

harshpatel
Contributor

Hi @nick405060 ,

Can you try this and tell me if this is what you are trying to get. I think your on click event handler is getting called every time but you are setting the value to true in each case which is not toggling the token value.

require([
    "jquery",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function ($, mvc) {

    // get default token model
    var tokens = mvc.Components.get("default");
    // set default value for token
    tokens.set("clicked", "false");

    $("#btn-submit").on("click", function () {
        console.log("changing token value from " + tokens.get("clicked") + " to " + (tokens.get("clicked")=="true" ? "false" : "true"));
        // toggle token value
        tokens.set("clicked", tokens.get("clicked")=="true" ? "false" : "true");
    });
});

View solution in original post

harshpatel
Contributor

Hi @nick405060 ,

Can you try this and tell me if this is what you are trying to get. I think your on click event handler is getting called every time but you are setting the value to true in each case which is not toggling the token value.

require([
    "jquery",
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function ($, mvc) {

    // get default token model
    var tokens = mvc.Components.get("default");
    // set default value for token
    tokens.set("clicked", "false");

    $("#btn-submit").on("click", function () {
        console.log("changing token value from " + tokens.get("clicked") + " to " + (tokens.get("clicked")=="true" ? "false" : "true"));
        // toggle token value
        tokens.set("clicked", tokens.get("clicked")=="true" ? "false" : "true");
    });
});

nick405060
Motivator

Took me a lot of debugging, but this works if you change "default" to "submitted"

Thank you!!

nick405060
Motivator
<form script="submitToken.js">
  <label>tester</label>

   <row>
     <panel>
       <html>
         <div>
           <input id="btn-submit" type="button" value="Click"/>
         </div>
       </html>
     </panel>
   </row>

  <row>
    <panel>
      <table>
        <search>
          <query>
| makeresults | eval message="$clicked$" | table message
          </query>
        </search>
      </table>
    </panel>
  </row>

</form>
0 Karma
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 ...