Dashboards & Visualizations

How to set initial value of text input in a form using eval or JavaScript..?

melonman
Motivator

Hi,

I am looking for a sample to set a initial value dynamically using eval or JavaScript.
e.g. set "Monday" or "Tuesday" as a initial value when a form is opened.

I have this very simple form like this:

<form>
  <label>sample</label>
  <fieldset submitButton="false">
    <input type="text" token="a">
      <label>Today</label>
      <initialValue></initialValue>
    </input>
  </fieldset>
</form>

and I want to set a value for depending on a day the form is accessed.
I am looking for a way to do this by eval tag or JavaScript extention, but I have not found any sample for this kind of input value initialization.

Any simple sample would be appreciated!

0 Karma
1 Solution

jeffland
SplunkTrust
SplunkTrust

You can give the input an id in Simple XML, get that input by its id in your js code and set its default setting. Should look something like this:

...
  type="text" token="a" id="textInput">
    <label>Today</label>
    ...

And in js:

...
var textInput = mvc.Components.get("textInput");
textInput.settings.set("default", "some value or dynamic text");

You can do the same with initialValue, though I doubt you really need that. Docs for that are here.

View solution in original post

jeffland
SplunkTrust
SplunkTrust

You can give the input an id in Simple XML, get that input by its id in your js code and set its default setting. Should look something like this:

...
  type="text" token="a" id="textInput">
    <label>Today</label>
    ...

And in js:

...
var textInput = mvc.Components.get("textInput");
textInput.settings.set("default", "some value or dynamic text");

You can do the same with initialValue, though I doubt you really need that. Docs for that are here.

melonman
Motivator

Thank you! Now I can start adding some more stuff for text box initialization ...

<form script="sample.js">
  <label>sample</label>
  <fieldset submitButton="false">
    <input type="text" token="token1" id="id1">
      <label>Text Input</label>
    </input>
  </fieldset>
</form>

$ cat sample.js
require(["splunkjs/mvc", "splunkjs/mvc/simplexml/ready!"], function(mvc) {
     var date1 = new Date();
     var year = date1.getFullYear();
     var val1 = mvc.Components.get('id1');
     val1.settings.set("default", year);
});
0 Karma

pradeepkumarg
Influencer

Create a lookup mylookup.csv like below
day_of_w, No
......................
Sunday ,0
Monday ,1
Tuesday ,2
.
.
Saturday,6

|inputlookup mylookup.csv | eval date_wday=strftime(now(),"%w") | eval No=if(date_wday==No,0,No) | sort No | table day_of_w

0 Karma

melonman
Motivator

Thank you for your input, and I understand what you showed. But could this search result be used for setting initial value for input as a text?

0 Karma

pradeepkumarg
Influencer

You can use a drop down and use
<selectFirstChoice>true</selectFirstChoice>

I'm sure there must be a way to populate text box as well

0 Karma
Get Updates on the Splunk Community!

Community Content Calendar, November Edition

Welcome to the November edition of our Community Spotlight! Each month, we dive into the Splunk Community to ...

October Community Champions: A Shoutout to Our Contributors!

As October comes to a close, we want to take a moment to celebrate the people who make the Splunk Community ...

Stay Connected: Your Guide to November Tech Talks, Office Hours, and Webinars!

What are Community Office Hours? Community Office Hours is an interactive 60-minute Zoom series where ...