Dashboards & Visualizations

How to set img src value in html panel based on token value?

j_quickbase
Explorer

I have a simple dashboard with a drop-down input field:

 

  <fieldset autoRun="false" submitButton="true">
    <input type="time" searchWhenChanged="false">
      <label>Default (5m realtime)</label>
      <default>
        <earliest>rt-5m</earliest>
        <latest>rtnow</latest>
      </default>
    </input>
    <input type="dropdown" token="instance" searchWhenChanged="true">
      <label>Instance</label>
      <choice value="prod">US</choice>
      <choice value="test">EU</choice>
      <default>prod</default>
    </input>
  </fieldset>

 

I would like to set the img src in the below HTML panel to specific URL, based on the value of the selected token above:

 

    <panel>
      <html>
         <img src="https://specific-url-based-on-the-token-value-above-html"></img>
      </html>
    </panel>

 

 
For example, if the "US" option is chosen, the img src URL should be: https://www.US-option.foo.bar.org

Or, if the "EU" option is chosen, the img src URL should be: https://www.EU-option.foo.bar.edu

I have tried "condition match", etc. and cannot seem to figure it out. Any guidance is greatly appreciated.

Labels (2)
Tags (2)
1 Solution

j_quickbase
Explorer

As this is in Splunk Cloud, and I'd like to keep all of the code inline (not worry about external/uploaded javascript, etc.)..I have managed to get the below to work;

 

  <fieldset autoRun="false" submitButton="true">
    <input type="time" searchWhenChanged="false">
      <label>Default (5m realtime)</label>
      <default>
        <earliest>-5m</earliest>
        <latest>now</latest>
      </default>
    </input>
    <input type="dropdown" token="instance" searchWhenChanged="true">
      <label>Instance</label>
      <choice value="prod">US</choice>
      <choice value="test">EU</choice>
      <default>prod</default>
      <change>
        <condition match="match(value, &quot;test&quot;)">
          <set token="country">https://www.EU-option.foo.bar.edu/image.png</set>
        </condition>
        <condition match="match(value, &quot;prod&quot;)">
          <set token="country">https://www.US-option.foo.bar.org/image.png</set>
        </condition>
      </change>
    </input>
  </fieldset>
  
  
    <panel>
      <html>
         <img src="$country$"/>
      </html>
    </panel>

 

Thanks to everyone that responded.

View solution in original post

0 Karma

j_quickbase
Explorer

As this is in Splunk Cloud, and I'd like to keep all of the code inline (not worry about external/uploaded javascript, etc.)..I have managed to get the below to work;

 

  <fieldset autoRun="false" submitButton="true">
    <input type="time" searchWhenChanged="false">
      <label>Default (5m realtime)</label>
      <default>
        <earliest>-5m</earliest>
        <latest>now</latest>
      </default>
    </input>
    <input type="dropdown" token="instance" searchWhenChanged="true">
      <label>Instance</label>
      <choice value="prod">US</choice>
      <choice value="test">EU</choice>
      <default>prod</default>
      <change>
        <condition match="match(value, &quot;test&quot;)">
          <set token="country">https://www.EU-option.foo.bar.edu/image.png</set>
        </condition>
        <condition match="match(value, &quot;prod&quot;)">
          <set token="country">https://www.US-option.foo.bar.org/image.png</set>
        </condition>
      </change>
    </input>
  </fieldset>
  
  
    <panel>
      <html>
         <img src="$country$"/>
      </html>
    </panel>

 

Thanks to everyone that responded.

0 Karma

thambisetty
SplunkTrust
SplunkTrust

@j_quickbase 

dashboard:

<form script="replaceimage.js">
  <label>testx</label>
  <fieldset submitButton="false">
    <input type="dropdown" token="instance">
      <label>Instance</label>
      <choice value="prod">US</choice>
      <choice value="test">EU</choice>
    </input>
    <input type="time" token="field2">
      <label></label>
      <default>
        <earliest>-24h@h</earliest>
        <latest>now</latest>
      </default>
    </input>
  </fieldset>
  <row>
    <panel>
     <html>
       <div id="image">
         </div>
     </html>
    </panel>
  </row>
</form>

 

js: 

$SPLUNK_HOME\etc\apps\search\appserver\static\replaceimage.js

require([
    "splunkjs/mvc",
    "splunkjs/mvc/simplexml/ready!"
], function (mvc) {
    // get default token model
    var tokens = mvc.Components.getInstance("default");
    tokens.on("change:instance", function(model, value) {
        console.log("Dropdown value changed: " + String(value));
    if (value=="prod"){
    	document.getElementById("image").innerHTML="<img src='https://homepages.cae.wisc.edu/~ece533/images/airplane.png'/>";
    }
    if (value=="test"){
	 document.getElementById("image").innerHTML="<img  src='https://homepages.cae.wisc.edu/~ece533/images/baboon.png'/>";
    }
    });
});
————————————
If this helps, give a like below.

renjith_nair
Legend

Try this,

<form>
  <fieldset autoRun="false" submitButton="false">
    <input type="dropdown" token="instance" searchWhenChanged="true">
      <label>Instance</label>
      <choice value="lights">Lights</choice>
      <choice value="forest">Forest</choice>
      <default>prod</default>
    </input>
  </fieldset>
  <row>
    <panel>
      <title>Image of $instance$</title>
      <html>
          <img src="https://www.w3schools.com/css/img_$instance$.jpg" alt="IMG" > </img>
      </html>
    </panel>
  </row>
</form>
---
What goes around comes around. If it helps, hit it with Karma 🙂
Get Updates on the Splunk Community!

Transforming Financial Data into Fraud Intelligence

Every day, banks and financial companies handle millions of transactions, logins, and customer interactions ...

How to send events & findings from AWS to Splunk using Amazon EventBridge

Amazon EventBridge is a serverless service that uses events to connect application components together, making ...

Exciting News: The AppDynamics Community Joins Splunk!

Hello Splunkers,   I’d like to introduce myself—I’m Ryan, the former AppDynamics Community Manager, and I’m ...