Copy link to clipboard
Copied
So, I feel like this is another one of those things that once I see it working, I won't believe I couldn't see the answer sooner.
In this case, I have a variety of check boxes, and if the check box is unchecked, there is no information (or a 0, if numerical) in the associated fields. I have the strands below working independently, but I can't find any sort of tutorials on how to make the Math.random function work inside of the if/else function. Alternatively, I would be fine with connecting all of the fields that require a variable output to a single button. Also, alternatively, I might want to include these variable outputs as part of a larger script, where a drop down menu with multiple options pushes data to several fields, and one or more of those fields are variable outputs.
I had originally thought that the function would replace the 'x' in the final set. Obviously, didn't work. So, any help now would be much appreciated.
function myFunction(){
var x = Math.floor(Math.random() * 10) + 1;
this.getField("RandomNumber").value =x;
}
if (event.target.value!="Off")
this.getField("Result2").value=10;
else this.getField("Result2").value=0;
if (event.target.value!="Off")
this.getField("RandomNumber").value=x;
else this.getField("RandomNumber").value=0;
Copy link to clipboard
Copied
Since it seems like what often happens with these forums is that an answer is given, but the minor details are left out, I thought I would wrap this one up by posting the end results.
Full credit for the resolution of this question should go to Try67, since it was his providing the missing nugget that made it all work.
Given a check box with 2 fields, one (RandomNumber) with a simple range of 0-9, and the other (Result2) with a range of 3-10,
this code is placed into a "Mouse Up" >>Use Java Script.
function getRand() {
var x = Math.random();
return x;
}
if (event.target.value!="Off")
this.getField("RandomNumber").value=getRand();
else this.getField("RandomNumber").value=0
function getRangeVal(lo,hi) {
var x = Math.floor(Math.random()*(hi+1-lo))+lo;
return x;
}
if (event.target.value!="Off")
this.getField("Result2").value=getRangeVal(3, 10);
else this.getField("Result2").value=0;
End result, check box with no check equals zero in both fields, but provides a random number with different ranges once clicked. In this example, you have to keep toggling the checkbox to change the number. I am still working on that, and I haven't explored Try67's suggestion that I don't need the 'function' yet. However, this is a functioning set of scripts, placed in the checkbox's Action tab>>Mouse up>>Run Javascript (in order to prevent a PDF with a lot of fields from having to run too many calculations and being slowed down considerably.
Try67, once again, thanks for your assistance.
Copy link to clipboard
Copied
Where does you call the function "myFunction" ?
Copy link to clipboard
Copied
Actually, I was wrong about that. I had the if/else working, the other bit was something I got from a forum, but there wasn't ever any discussion around where it was used or how. Which is part of why I am asking the question I need answered instead of just trying to adapt what I read this time. Nothing is close enough.
By the way, it is important to note that the end result of this project will have hundreds of fields, so I am trying to steer away form active calculations bogging down the PDF. It already has like 4 or 5 counts on the wait icon.
Copy link to clipboard
Copied
What is your aim, in detail?
Copy link to clipboard
Copied
You can't use a variable that is defined inside a function from outside of it, unless you return its value from within the function.
For example:
function getRand() {
var x = Math.random();
return x;
}
event.value = x; // WON'T WORK!
event.value = getRand(); // WILL WORK
Copy link to clipboard
Copied
Thanks, Try.
I assume that the script goes into the button's mouse up "Run a JavaScript" option? Also, do I just replace the 'x' with the field name? (in this case Result4)
Copy link to clipboard
Copied
Yes, and no. You need to replace lines 6 and 7 above with this:
this.getField("name of target field goes here").value = getRand();
Copy link to clipboard
Copied
This worked!
function getRand() {
var x = Math.random();
return x;
}
this.getField("Result4").value=getRand();
Now, for the second half of it, how do I squirrel it into an if/else statement, such that it only generates a number if the check is marked?
Is that this?
function getRand() {
var x = Math.random();
return x;
}
if (event.target.value!="Off")
this.getField("RandomNumber").value=getRand();
else this.getField("RandomNumber").value=0
Copy link to clipboard
Copied
I would do it differently. Move the code to be the custom calculation script of the text field and change it to:
event.value = (this.getField("Name of check-box goes here").valueAsString=="Off" ? 0 : Math.random();
Copy link to clipboard
Copied
Doesn't that cause it to be constantly calculated (well, everytime a field changes)? I am concerned about too many calculation bogging this thing down.
Either way, I appreciate your help and patience while I learn this stuff.
Copy link to clipboard
Copied
You're right. It won't work well in this scenario, so the code you posted above should work.
You don't need the function, by the way, since it just does one thing...
Copy link to clipboard
Copied
Since it seems like what often happens with these forums is that an answer is given, but the minor details are left out, I thought I would wrap this one up by posting the end results.
Full credit for the resolution of this question should go to Try67, since it was his providing the missing nugget that made it all work.
Given a check box with 2 fields, one (RandomNumber) with a simple range of 0-9, and the other (Result2) with a range of 3-10,
this code is placed into a "Mouse Up" >>Use Java Script.
function getRand() {
var x = Math.random();
return x;
}
if (event.target.value!="Off")
this.getField("RandomNumber").value=getRand();
else this.getField("RandomNumber").value=0
function getRangeVal(lo,hi) {
var x = Math.floor(Math.random()*(hi+1-lo))+lo;
return x;
}
if (event.target.value!="Off")
this.getField("Result2").value=getRangeVal(3, 10);
else this.getField("Result2").value=0;
End result, check box with no check equals zero in both fields, but provides a random number with different ranges once clicked. In this example, you have to keep toggling the checkbox to change the number. I am still working on that, and I haven't explored Try67's suggestion that I don't need the 'function' yet. However, this is a functioning set of scripts, placed in the checkbox's Action tab>>Mouse up>>Run Javascript (in order to prevent a PDF with a lot of fields from having to run too many calculations and being slowed down considerably.
Try67, once again, thanks for your assistance.
Copy link to clipboard
Copied
Alright, so I gave it a whirl, and I am not sure how to accomplish the random result without the function. would you mind lining me out on that.
Also, do I need to start another thread for the next 'experiment'? I want to build on this, and then take a list of ten to twenty items all controlled by checkboxes. Each checkbox will have a set of fields to populate, but given the number, I wouldn't want to have to continuously check and un-check each box to get the random variables. What I want to do instead is have a button that says run random number in each of these fields (only about 1/4 of the fields wouldn't be fixed), but only if the box is checked, returning 0 for unchecked. Using this:
function getRangeVal(lo,hi) {
var x = Math.floor(Math.random()*(hi+1-lo))+lo;
return x;
}
if (this.getField(:CheckBox2")!="Off")
this.getField("Result2").value=getRangeVal(3, 10);
else this.getField("Result2").value=0;
I get the result regardless of the checked status when the button is pushed. The goal here is to have a single button run several of these math.random fields, which can then all be summed in a total field. Catch is, some fields have a static number, some will have random, all with different ranges.
Copy link to clipboard
Copied
Don't worry about the function thing, it's not a big deal.
Just for reference, you can replace this:
function getRand() {
var x = Math.random();
return x;
}
if (event.target.value!="Off")
this.getField("RandomNumber").value=getRand();
else this.getField("RandomNumber").value=0;
With this:
if (event.target.value!="Off")
this.getField("RandomNumber").value=Math.random();
else this.getField("RandomNumber").value=0;
Copy link to clipboard
Copied
This line is incorrect:
if (this.getField(:CheckBox2")!="Off")
If you're placing this code as the Mouse Up event of "CheckBox2" then you should use the code I provided before, ie:
if (event.target.value!="Off")
Copy link to clipboard
Copied
To address both of your answers,
1st. That worked (though, I could swear I tried exactly that). Can the same thing be done with the get range function?
2nd. In this expansion of the original question, I think I need the script to be in the mouse up action for the button, don't I? I just need to have the check mark indicate yes/no or there/not there... 1 or 0, I guess.
Which, I suppose, might mean that I could simply multiply the random number by checkbox output, making that 1 on and 0 off.
Copy link to clipboard
Copied
1. Yes
2. Correct, although multiplying by "Off" is not the same as multiplying by zero... If you do that you will get NaN as the result (Not a Number).
Copy link to clipboard
Copied
Alright, so. I have this working, in the button. Now, how do I make it NOT work if the checkbox is clicked?
this.getField("Result5").value=Math.floor(Math.random()*(10+1-3))+3;
if (this.getField("CheckBox2")!="Off") \\everything so far suggest this patter, but it doesn't work.
else this.getField("Result5").value*0;
The button should be able to run 10-20 (or 100, for that matter) of these field calculations, but how do I make it come back 0 if the check box assigned to a field is not checked? I have tried doing a variable set up, but it is likely I was doing it incorrectly.
Copy link to clipboard
Copied
You just need to combine them, like this:
if (this.getField("CheckBox2").valueAsString!="Off")
this.getField("Result5").value = Math.floor(Math.random()*(10+1-3))+3;
else this.getField("Result5").value = 0;
Edit: code fixed
Copy link to clipboard
Copied
Sorry, there was an error in the code above. I fixed it now.
However, if you're placing the code as the mouse up event of ChecBox2 then it needs to be:
if (event.target.value!="Off")
this.getField("Result5").value = Math.floor(Math.random()*(10+1-3))+3;
else this.getField("Result5").value = 0;
Copy link to clipboard
Copied
Try67,
taking what you have helped me figure out, I expanded to using a drop down field. Using this sequence, it worked.
if (this.getField("ItemSelection").valueAsString=="No variable")
this.getField("Output").value=0;
else if (this.getField("ItemSelection").valueAsString=="Variable1")
this.getField("Output").value=Math.floor(Math.random()*(4+1-1))+1;
else if (this.getField("ItemSelection").valueAsString=="Variable2")
this.getField("Output").value=Math.floor(Math.random()*(8+1-5))+5;
else if (this.getField("ItemSelection").valueAsString=="Variable3")
this.getField("Output").value=Math.floor(Math.random()*(12+1-9))+9;
else if (this.getField("ItemSelection").valueAsString=="Variable4")
this.getField("Output").value=Math.floor(Math.random()*(16+1-13))+13;
Just thought you might appreciate that I am taking what you teach and figuring out other bits with it.
Copy link to clipboard
Copied
Great!
Copy link to clipboard
Copied
I have seen that value as string thing before, I thought it was just a way of saying 'use as is'. Once again, thank you, good sir.

