How to put a variable inside quotes JS [duplicate] - javascript

This question already has answers here:
How do I put a variable in a string using javascript
(4 answers)
jQuery passing element ID into jquery statement?
(6 answers)
How to interpolate variables in strings in JavaScript, without concatenation?
(17 answers)
Closed 4 years ago.
I'm trying to select a specific audio element based on the key that's pressed. The issue is the compiler is reading the variable as a string. How can I get it to read it as a variable?
var hail = e.keyCode
var afile = document.querySelector("audio[data-id='hail']")

You can use Javascript template strings :
var hail = e.keyCode
var afile = document.querySelector(`audio[data-id=${hail}]`)

Related

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

Calling Javascript variable inside javascript code [duplicate]

This question already has answers here:
How can I do string interpolation in JavaScript?
(21 answers)
Closed 6 years ago.
I have the following code
dt.ajax.url( 'test.php?status=' ).load();
I defined a variable
var status= 55
I wand to add status_2 in the link like this
dt.ajax.url( 'test.php?status=&status' ).load();
How can I do it ?
Try this. You need to concatenate the variable with url
dt.ajax.url('test.php?status='+status).load();

How would this be done in JavaScript. Using a variable in a concatenate string [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 6 years ago.
Ok. The code here is in fact Javascript but I can not find a way to fixing the problem in javascript. The code below is javascript.
var =inputdata;
dataout = data.items.inputdata.time
This is how it would sort of look like if it was php
dataout = data.items.$inputdata.time
I would like inputdata to be treated as a variable and not as text.
Sorry for the small amount detail
You can use square bracket notation:
dataout = data.items[inputdata].time
This will allow you to use a string in place of a key for a javascript object.

Calling a jQuery function using a variable name [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 7 years ago.
I have the following code where MODE is a variable. Its value should be a method of jQuery's tinycolor liberary.
$cor = tinycolor($cor).mode(z).toString();
I'd like to call that method on that line so, for instance, when mode = 'lighten' $cor would be
$cor = tinycolor($cor).lighten(z).toString();
Is there a way of doing it this way?
Thanks!
Use bracket notation
$cor = tinycolor($cor)[mode](z).toString();

Append variable value to form Input [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 8 years ago.
I need to append numeric value stored in one variable to form input fields.
Let's Say
var b= 5;
var text=$("#dropdown_id").val();
I want to append value of variable b in dropdown_id .
My expected result is #dropdown_id5
Use string concatenation:
$("#dropdown_id" + b).val()

Categories