Hi guys, I've been having this problem for a while now. I have a script that generates a hash for a file based on the file that was uploaded, and it then puts that hash into the input field. If you inspect the element on the page I can see the value has changed to the hash. But in order to make it search, I need to type in the field.
Any ideas?
Thanks.
Update: Working code, thanks to @niketnilay!
DashboardController.ready();
pageLoading = false;
$(document).ready(function () {
console.log("Step 1. Inside Document ready function");
$(document).on("click","#btn",function () {
console.log("Step 2. Button Clicked");
var reader = new FileReader(); //define a Reader
var file = $("#f1")[0].files[0]; //get the File object
if (!file) {
alert("no file selected");
return;
} //check if user selected a file
reader.onload = function (f) {
console.log("Step 3. Reader on load function");
var file_result = this.result; // this == reader, get the loaded file "result"
var file_wordArr = CryptoJS.lib.WordArray.create(file_result); //convert blob to WordArray , see https://code.google.com/p/crypto-js/issues/detail?id=67
var sha1_hash = CryptoJS.SHA1(file_wordArr); //calculate SHA1 hash
//alert("Calculated SHA1:" + sha1_hash.toString()); //output result
$('#input1_3519-input').attr('value', sha1_hash);
setTimeout(function(){
console.log("Step 4 updated token values");
defaultTokenModel.set("form.search_hash_url", sha1_hash);
defaultTokenModel.set("search_hash_url", sha1_hash);
},10);
};
reader.readAsArrayBuffer(file); //read file as ArrayBuffer
});
});
}
);
// ]]>
@JarrenJ, would it be possible for you to add the code for event which is used to set the textbox value?
If there are no events then you can use setTimeout() JavaScript function to set both form.search_hash_url
(which should update the value in Splunk Text Box input) and search_hash_url
(which should set the token used in dashboard for running search).
As you can see there is no need to depend on jQuery to use attr()
function to set the value for Splunk Text Box Input. I was under impression that you are using HTML text box.
Also, if you are using HTML dashboard and not Simple XML JS Extension on top of Simple XML Dashboard, you should search the code as HTML dashboard by default get both defaultTokenModel
and submittedTokenModel
. Which implies you do not have to explicitly call var defaultTokenModel=mvc.Components.get("default");
. Please try out the following code and confirm:
setTimeout(function(){
//Set the Splunk Text Input value using token form.search_hash_url
defaultTokenModel.set("form.search_hash_url","testing");
//Set the token for Splunk Dashboard search using token search_hash_url
defaultTokenModel.set("search_hash_url","testing");
},10);
Following should work as well based on what you are trying, however, above approach with form.search_hash_url
and search_hash_url
is better since you are using Splunk Text Input not HTML.
setTimeout(function(){
// Set the value for Splunk Text Input using jQuery attr() function
$("div#input1 div.splunk-textinput input").attr("value", "testing");
//Set the token for Splunk Dashboard search using token search_hash_url
defaultTokenModel.set("search_hash_url","testing");
},10);
I have used setTimeout()
JavaScript function for example. You should code this inside that change event for file hash name, when it gets populated.
@niketn, The token that used as the "value" for my submit is $form.search_hash_url$. I tried setting it in my script according to the link you gave me by doing:
`
var tokens = mvc.Components.get("default");
var tokenValue = tokens.get("form.search_hash_url");
// Change the value of a token $mytoken$
tokens.set("form.search_hash_url", "testing");
`
And it didn't work.