Append variable value to form Input [duplicate] - javascript

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()

Related

How to put a variable inside quotes JS [duplicate]

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}]`)

Name of a variable as string, how to get value? [duplicate]

This question already has answers here:
Convert string to variable name in JavaScript
(11 answers)
Closed 6 years ago.
A javascript newbie question:
Let's say, there is a variable defined as follows:
var var1 = "Something"
On the other hand, I generate such strings (simplified):
var nmbr = "1"
var varname = "var" + nmbr
Now I have to get the value of a variable with such a name (varname as string).
document.write([???varname???])
should write 'Something'.
How to do this?
console.log(variable_name);
will print the variable in the console.

Pass a variable in a selector jquery [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
jQuery: using a variable as a selector [duplicate]
(1 answer)
Closed 7 years ago.
I want to pass a variable in a selector jquery but I don't find the right syntaxe.
I have this :
var jsonobject = eval(responseObject);
for(var item in jsonobject)
{
$('#",jsonobject[item],"').css("background-color","red");
}
So the variable is jsonobject[item] and I want to use the value in it as the name of the selector.
Thanks for your help !
You need to use
$("#"+jsonobject[item]).css("background-color","red");
Simply use string concatenation for that:
$('#' + jsonobject[item]).css(...);

Set dynamic value to Array in JS like PHP [duplicate]

This question already has answers here:
How to insert an item into an array at a specific index (JavaScript)
(28 answers)
Closed 7 years ago.
My question is how I will set a value an array in JS like PHP?
for example:
in php I have an array named Arr, I set a new value in n position using
Arr[] = value
but I try to do same in JS but display an error
Use the push command
Arr.push(value)

How can I turn a string into a key? [duplicate]

This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.
var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);

Categories