How to insert value from function to variable javascript - javascript

i want to add the value of my function to the variable.
for example i have something like this
<input v-model="search.steering" #input="onChange('steering')"/>
that mean i want to insert the steering each i input the text.
here the function
onChange(typemember) {
this.search.typemember
}
what im doing here is i add the typemember value to this.search variable.
so the variable in javascript now will be like this this.search.typemember the typemember value is taken from #input="onChange('steering')"
in this example the variable now will be like this this.search.steering. is that posible? or is there anothere way to doing this?
btw im using vue here.

To access this.search.steering dynamically using typemember you'd just need to use square brackets:
this.search[typemember]

Related

Alternative for document in document.getElementById('ID')

I am completely a beginner in java script. As i know, by using this line of code:
document.getElementById('ID').value
It looks in the entire PHP file to find the value of that id (it's a text input). Is there any alternative for the documents on that line of code that let me to search only in a class?
for example i have a class called number, so :
this.block = $("#number");
can i use something like that:
this.block.getElementById('ID').value;
Or there is any another line of code that give me the value of a text input? Thanks a lot
You can use
$("#number").find("#id")
to search descendants of #number.
But if your class is number you should use $(".number") instead since that is the class selector.

Angular 2 - Display variable inside a variable in a template

I'm quite new in Angular 2, and I want to show in my template a string in a variable that has to contain inside another variable. I will show you a simplified example of what my problem is and what I want to achieve:
questionnaire.component.ts
/* Starts being "", populating an input text will modify this */
name = "Albert";
/* This variable comes from calling an API, here I just put it as created to simplify */
question.title = "Hello {{name}}, how old are you?";
questionnaire.template.html
<p>{{question.title}}</p>
The result I'm getting is:
Hello {{name}}, how old are you?
and my desired result would be:
Hello Albert, how old are you?
I have tried to escape the "{{ }}" in the string stored on my DB, used the ASCII character instead of the curly braces, put it inside [innerHTML]... but the result was always the same.
Do you know how can I solve this?
Thank you very much!
{{}} only works in Angular component templates and not in arbitrary strings and also not in HTML added dynamically to the DOM.
Just change it to
question.title = `Hello ${this.name}, how old are you?`;
to use TypeScript string interpolation.
In angular2 you have to use this keyword
For example, ${this.name}

How to properly use jquery to set a value using $.post if html value only has a name?

I'm new to jQuery and JS, so perhaps this is a very simple question, but I don't quite understand how to use the $.post() function for jQuery. What I'm trying to do is change a value of an input in an html file using jquery.
For example, let's say I'm trying to post to some test site, and change the value of my_data ( the name of an attribute ), which is originally at 1.
I figured I'd use $.post for this, so I have something like:
$.post("http://test_site.com/", function(my_data) {
$(#my_data).val(5));
});
However, I think that # is for the id? I'm not sure if I'm just misunderstanding post, or not. Any hints / points in the right direction would be greatly appreciated!
You are missing quotes and name selector should be: $([name='YOUR_ATTRIBUTE_NAME'])
Try this:
$("[name='my_data']").val(5);

Qualtrics: Transform piped variable using javascript and display it in text?

Let there be a piped variable in qualtrics (a variable holding values recalled from a previous input by a survey respondent), how do I transform it to lower-case before displaying it?
There is the java-script function .toLowerCase(); but how do I change a variable in the qualtrics custom javascript and pipe it back into a survey text?
This link explains how to use piped variables in javascript. Say I define a new variable in javascript like this
// my piped variable is ${q://QID12/ChoiceGroup/SelectedAnswers}"
var selectedChoice = "${q://QID12/ChoiceGroup/SelectedAnswers}";
var selectedChoiceLower = selectedChoice.toLowerCase();
How to pipe selectedChoiceLower back into a question text?
Any ideas?
Use the setEmbeddedData() function to assign selectedChoiceLower to a new or existing embedded data variable (see documentation). Then, you can use the embedded field variable in a later question.
javascript: setEmbeddedData('lower', selectedChoiceLower);
embedded field code: ${e://Fields/lower}
warning: putting user-generated text into your javascript is very dangerous! Qualtrics does not do any escaping. If a user puts a ' or " in their text, it will be put directly into the javascript and probably break your code. Code injection is also possible. See this question for more details.

Trying to access a <c:set variable in javascript

I have this line in the header of my JSP <script>var logicalName = "${logicalName}"</script> which is resolving to the correct value.
I want to access this value via javascript/jquery. How do I do this?
This is not working console.log($logicalName);
You just need to remove the string symbol from your parameter
console.log(logicalName);
console.log("${logicalName}");
and
console.log(logicalName);
Both will work!

Categories