Skip to main content
Nintex Community Menu Bar

Why isn't this Javascript code working in Data Label Expression?

  • Dec 2, 2016 12:34 PM UTC
  • 8 replies
  • 68 views
  • Translate All

Forum|alt.badge.img+16

Any ideas why this code isn't working when added to an expression on a data label with Literal ticked?

It's supposed to generate a random alphanumeric and return the value to the data label to be displayed.... I just get a blank..

I tested the code via an online HTML tester and it works, but just doesn't in k2.... any ideas?
Originally i tried in K2 designer with an expression using the Random function, but it is limited. I need a Fixed length alphanumeric, is this possible with K2 Deisgner?


Thanks all

 

Sorry, i had to insert pic, because this post keeps getting deleted if i do insert code for some strange reason...

 

13126i3B29836558A64B20.jpg

8 replies

Forum|alt.badge.img+8

Dear

 

Please try wrapping your javascript code with $( document ).ready(function() {});

 

Regards

Translate

Forum|alt.badge.img+16
  • Author
  • Scout
  • Dec 2, 2016 4:11 PM UTC

Had a go, still comes back empty:

 

15053iD97B4783A4D545E8.jpg

 

Is my code wrong?

 

Translate

Forum|alt.badge.img+8

Dear 

 

Try again but wrap it with

window.onload = function() {

//Your Code Here

}

 

 

Regards

Translate

Forum|alt.badge.img+8

Dear Sharp 

 

Ran some testings on your code , there was on problem with the DOM only .

here is the code (tested) should be working fine for you .

 

<script>
window.onload = function() {
debugger;
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
var word = '';
while (word.length < 5){
var rand = Math.floor(Math.random() *characters.length);
addchar = characters.substring(rand,rand+1);
if(word.length == 0 && addchar >= 0 && addchar <= 9){}else{word+=addchar}
}
document.getElementById("result").innerHTML = word;
}
</script>

 

Regards.

Translate

Forum|alt.badge.img+16
  • Author
  • Scout
  • Dec 5, 2016 4:08 PM UTC

With Ahmads help, got this working upon initialization.

 

I've tried to do a Save on the form, but the Save Method is saving the Data Label Javascript as the contents, rather the result of the javascript.i i.e my random number is 02as4 for example on screen, but the save method saves the javascript CODE itself.

 

Any ideas how to save the result? I tried transfer etc but no joy.

 

Thanks

Translate

Forum|alt.badge.img+18
  • Dec 5, 2016 8:23 PM UTC

I am not sure if this is the only way/or the correct way, but I have seen this works when attempting to set the text for a data label control:


 


var aTextField = "testing";


$("span[name='DataLabelName']").SFCLabel("option", "text", aTextField);


 


I think effectly finding the span with the data label according to its name and using this SFCLabel function to set the text

Translate

Forum|alt.badge.img+16
  • Author
  • Scout
  • Dec 6, 2016 12:13 PM UTC

Hi Tin, Thanks for the info... with Ahamd's direction, i added the line and it looks like this: my data lable is called 'String Data Label'... but the result is an empty box.

 

<span id = "result"></span>
<script>
window.onload = function() {
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"+"abcdefghijklmnopqrstuvwxyz";
var word = '';
while (word.length < 5){
var rand = Math.floor(Math.random() *characters.length);
addchar = characters.substring(rand,rand+1);
if(word.length == 0 && addchar >= 0 && addchar <= 9){}else{word+=addchar}
}
$("span[name='String Data Label']").SFCLabel("option", "text", word);
}
</script>

Any ideas?

Translate

Forum|alt.badge.img+16
  • Author
  • Scout
  • Dec 6, 2016 12:16 PM UTC
Thanks Tin, With your code and Amhads direction and a little tweeking got this working. For anyone else, here's the solution. I used Tins code, but added another Holding Data Label to the Form. Run the expression containing your javascript from the Holding Data Label. Then change Tin's one-liner to direct the output to your original Data Label name. I was then able to run a SAVE METHOD, and it successfully saved my Random Number..... phew I think the Expression and the Javascript on the same Data Label were fighting each other and it ends as a DRAW. So directing the javascript to a different Data Label ensures all parties are happy. Big thanks to Ahmad and Tin !!!!
Translate