Computed Properties to update variable in Vanilla JavaScript - javascript

Hey everyone, i need help with something.
I've divided the picture into sections so it's easier to look at.
This is really bugging me and i don't know if i can solve it this way.
Thanks to anyone that can help me... Here i go:
So in section 1 i've created a basic input with a name property, actually i've made 4 of them but this one is the example (the name property is important)
In section 2 , those are the inputs on the page and i need to change the variable depending if the inputs is checked or not
The variable in JS file is called requireInteraction and it's set to false
So im doing a forEach on those 4 inputs (each one has a name property that matches the variable name in JS) and i want to change the variable in JS if the checkbox with that name is Checked. I tried using Computed Properties.
So when silent is checked (with the silent name property) i want the variable "silent" in JS to switch to true.
How can i extract the input name (which has the same name of the JS variable) and make it so it's like i actually typed "silent = true" and changed the JS variable.

I don't really know why you can't use
requireInteraction = true;
but here, in case that you have some reason you can't just use the variable directly, you could use an object to serve as a target to index into:
const context = {
requireInteraction: false,
};
Then later on, you can use something similar to your original code:
context[el.name] = true;

Related

GTM - Truncated DataLayer Variable into Custom Javascript Variable

I currently have a dlv variable to store "First Name" (gtm.element.2.value) which is working correctly.
I also a dlv to store "D.O.B." which is also working correctly - gtm.element.5.value (this is formatted MM/DD/YYYY).
However, I'd like to only show the first initial in the First Name dlv and the Year in the DOB dlv. I'm thinking of utilizing a Custom JS variable but am open to ideas if there is an easier route.
Can anyone help provide what that Custom JS variable would look like? I've been searching for some examples but not having luck with this specific example.
Appreciate the help in advance!
To get the first initial (i.e. first character) of the First Name variable, you can indeed use a Custom JavaScript variable with this:
function() {
return {{first_name}}[0]; // Replace with the actual DLV reference
}
Similarly, to get just the year (YYYY) of the D.O.B., you can use a Custom JS variable:
function() {
return {{date_of_birth}}.split('/').pop(); // Replace with the actual DLV reference
}
Obviously you might want to add some checks to make sure the input is in a predictable format. For example, you might want to check that {{first_name}} is a string of non-zero length, and you might want to check that {{date_of_birth}} actually contains a date string with slash as the separator.

How to copy the value of a yform to an other field

In our (hybris) shop some products have a yform to summarize the parts of the product. Is there an easy way to copy the value of the sum field into an other field (automaticly) like the productquantity (no yForm)?
I guess I need javascript, but the id of the sumfield is generatad, so I don't know how to get the sum. Also my Javascript abilities are quite limited...
UPDATE:
To get the value I use this part of code:
copyYFormValueToProductQuantity : function() {
var copyText = document.querySelector('input[id*="sum"]').value
if (copyText > 0 && copyText != null)
{
//do stuff
}
console.log("Copied value: " + copyText)
},
But this line
document.querySelector('input[id*="sum"]').value
returns null. If I use it in the browserconsole, it also returns null. But after I inspected the element it works and returns the value I want to. So I guess I am missing some JS-basics here and the object isn't ready before?
Btw.: I call this function with a keydown-eventlistener.
This can most likely be done from the jsp files. There you have all the data that is needed, so you most likely need to only copy that field where you need it.
We can also help you more if you add some code examples here (what exactly is that yform?). If you struggle to find where in code is that specific yform created/added, it's always worth giving a try to search for the applied classes of the html (search the entire project and see what you find).
As I understand your question, you are saying that you want to copy the value of a yForm field named sum into a non-yForm field named productquantity, and you are asking specifically about how to access the value of a yForm field from JavaScript. If I understand this correctly, you can do so by calling the following JavaScript API:
ORBEON.xforms.Document.getValue("sum");
You can find more about this and other related API on Client-side JavaScript API.

How to get value of Qualtrics Drag & Drop Choice using Javascript

I have what should be a simple answer. I have a drag and drop style multiple choice question on Qualtrics. I have recoded the answers as I wish. I think it assigns to each answer a variable like QID15_1 QID15_2 QID15_3, etc. The outputted data then gives the rank order of that selected option.
So if I ranked them, for example
QID15_3
QID15_1
QID15_2
The value of QID15_3 =1, the value of QID15_1=2, and the value of QID15_2=3.
What is the correct syntax to access these values? I want to set an Embedded Data item with the value of QID15_1, for example (so it should equal 2), but I can't seem to get it correct.
I've tried Qualtrics.SurveyEngine.getSelectedAnswerValue and things like this.
Any help would be appreciated. Thanks!
EDIT: With the help of a friend who knows Java, we figured it out. You can write something like
Qualtrics.SurveyEngine.addOnUnload(function()
{var order=document.getElementById( 'QR~QID15~1' ).getElementsByClassName( 'rank' )[0].innerText;
}
You don't need to set an embedded variable in JavaScript. You can just pipe the value wherever you need it (in a subsequent question, in the survey flow, etc.). It would be:
${q://QIDx/ChoiceNumericEntryValue/y}
where x is the question id and y is the choice id.
EDIT:
You can assign it to an embedded data variable in the survey flow like this:
edvar = ${q://QIDx/ChoiceNumericEntryValue/y}

JavaScript: Accessing Variables from Other Files and Changing their Properties

So I am making a hacking type game coded with HTML and JavaScript, and I made a Store.html file for my store to purchase upgrades for your hacking system. I have several variables that I want to view and change from a different file than the one that I originally have it inside of.
Variable 1
var tutorialDone = false
Variable 2
var jobBoolean
Variable 3
var bitCoins = 30
var currentBit = bitCoins
I want this to be false because when the tutorial mission is finished, then I want to change the jobBoolean variable to "true" because when the tutorial mission is completed, then I want a new list of missions/jobs to show up on thee screen. The first mission is a seperate page. At the end of the mission when it is completed, then "tutorialDone" I want to be equal to "true", so that when I am back at the homepage, I can change the jobBoolean to true. Because the "tutorialDone" variable is dependent on the "jobBoolean" variable. When "tutorialDone" equals "true" so does "jobBoolean". I don't know how to make "tutorialDone" change along with the "jobBoolean".
I have for the third variable, there is two. That is because they are similar. I have a function inside the "Store.html" document that when an item is purchased, it grabs a variable which in this case is "currentBit". "currentBit" is equal to "bitCoins" because I thought that if I made a new variable, "currentBit", then set it equal to "bitCoins", it would connect with "bitCoins" from a seperate file. This was my only "solution" I could think of, but it doesn't work.
Also, as a bonus answer for you guys, how can I make an "if" statement in JavaScript, and then have it check if the variable I give it is equal to something, and if it is, run some code. Because whenever I try it, the function the if statement is in, it will run through the if part of the statement no matter if the statement is true or false.
Sorry if this question doesn't make much sense. This is just the first big problem I have encountered with JavaScript, that I couldn't fix myself.

Using text input by a user to reference a variable

first time I believe I have posted in stackoverflow and I hope I can explain what I would like to try and accomplish.
To try and phrase my question the most concisely, is there a way I can have a user input a string of words into a form in HTML, then use Javascript to check to see if any of those words match a variable that is already declared and then reference that variable so that I can use the value that is saved there?
For example we will have var feet = 12 declared, and a user inputs the word "feet" into a text field. I want to be able to take the user input and use it to reference the variable for later purposes in the code.
Hopefully explains my scenario well enough and someone can offer some advice
Thanks!
If the variable is a global variable, you can use window[varname] to get the value of the variable whose name is in varname.
Do you really need to allow the user to access any random Javascript variable? If not, a better approach would be to use an object to hold the data you want to allow the user to access. E.g.
var units = {
feet: 12,
inches: 1,
...
}
var conversion_factor = units[user_input];

Categories

Resources