I have a function that takes 2 inputs, the variable name as a string and the variable itself - it prints both via console.
var variable1 = ["entry1","entry2"];
function logthis(input1, input2){
console.log(input1+ "\n " + input2)
}
logthis("variable1", variable1) ;
I want to be able to use it as logthis(variable1); and get the same result.
How can I reference the variable name (as a string) and not its contents?
eg, console.log(input2.name) will not work
Something like the C+ equivalent of "nameOf(variable1)"
var variable1 = ["entry1","entry2"];
function logthis(input1, input2){
console.log(input1+ "\n " + input2)
}
logthis('variable1', variable1) ;
You are sending the reference. Pass first argument as string.
Try to get your input from the table first
function logthis(inputTable){
var input1 = inputTable[0];
var input2 = inputTable[1];
console.log(input1+ "\n " + input2) // will display "entry1 \n entry2 "
}
var table1 = ["entry1","entry2"];
logthis(table1);
Related
I've looked all over online for something that could aid me to fill an empty array with given values the user inputs from a text box that will get stored inside an array.
So far I have the following code:
var text = document.getElementById("input").value;
var message = document.getElementById("text-here");
message.innerHTML += text + " " + "<br />" + "<br />";
var x = [];
x.push(text);
console.log(x);
When I input something in the textbox and see what happens in the console it tends to replace the previous value that was sent there first.
For example, if I wrote "Hello", this will get sent into the array so it'll be seen as:
["Hello"]
But if I type in something again, hoping that the result will continue to store the data being inputted inside, it does this:
*Writes down "Hi" in the text box:
["Hi"]
I want the result to be something like this:
["Hello", "Hi"]
I am aware my code does need tweaking and I am doing something wrong which is causing that result, but I can't seem to figure it out.
I'm looking for an answer in vanilla JavaScript.
Thank you.
The problem is you are redeclaring variable x and initializing it with an empty array, every time when you run that code. Make the x a global variable by moving it out of the current function or block
var x = [];
Another Block
var text = document.getElementById("input").value;
var message = document.getElementById("text-here");
message.innerHTML += text + " " + "<br />" + "<br />";
x.push(text);
console.log(x);
var x = [];
function() {
var text = document.getElementById("input").value;
var message = document.getElementById("text-here");
message.innerHTML += text + " " + "<br />" + "<br />";
x.push(text);
console.log(x);
}
something like this should fix your problem. You were declaring x as an empty array every time you ran your javascript which would reset it to an empty arry.
I'm getting undefined instead of a String on return of the answer - though it is the right correct characters being logged. How do I get it to output a definite string?
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
You forgot to return a value, so the return-value is undefined.
var greet = function(name) {
let first = String(name. charAt(0). toUpperCase());
let second = String(name.slice(1));
return 'Hello ' + first + second + '!';
}
console.log(greet('Gemma'));
The console will print the result of evaluating an expression. You can notice if you set
let name = 'john'
it will print undefined in the very next line.
That is also happening here. First it is printing your value, then it print undefined.
Your function is working fine - you just need to invoke it and feed its argument with a name...
[for the result look in the console]
var greet = function(name) {
let first = String(name.charAt(0).toUpperCase());
let second = String(name.slice(1));
console.log('Hello ' + first + second + '!');
}
;
greet("henky");
I have a function that I need to use for filtering table rows:
setFilterString("Filter");
But I have a problem. I can set it to
setFilterString("OrderID = 5");
and it will filter out row where OrderID is equal to 5 but if i try using a variable that has a value taken before like this
setFilterString("OrderID = vOrderID");
I get error "Invalid column name 'vOrderID'." (as vOrderID is variable and not a column, I guess)
I have seen somewhere in filter section inputting something like this ("OrderID = '" & vOrderID & "'") but it doesn't have any result at all for me. Doesn't even throw any error in the console.
JavaScript assumes you are just passing a string to the function. If you want to use the variable, you should try this:
setFilterString("OrderID = '" + vOrderID + "'"); // Results in OrderID = '5'
or
setFilterString("OrderID = " + vOrderID); // Results in OrderID = 5
depending on the body of your function.
Use + instead of &: setFilterString("OrderID = " + vOrderID) should work.
Use "+" for merge strings:
setFilterString("OrderID = " + vOrderID)
You can also try to use ${idvOrderID} inside string:
setFilterString("OrderID = ${vOrderID}")
Or:
setFilterString(sprintf("OrderID = %s", vOrderID))
Remember about difference between ' and "
This question already has answers here:
Access Javascript variables dynamically
(4 answers)
Closed 8 years ago.
I know there are a lot of questions about if it is possible to use variable variables in jQuery.
One of the questions is this one: click here.
I tried to use the answer, but I don't know how I can use it in my case.
var numberofquestions = 10;
var dataString = "";
for ( var i=1; i<=numberofquestions; i++ ) {
/* ------ first part ------- */
if (i==1) {
dataString = dataString + "q1=" + question1 + "&";
} /* ------ end first part ------- */
else if (i == numberofquestions) {
questionValue = "question" + numberofquestions;
qValue = "q" + numberofquestions;
dataString = dataString + qValue + "=" + questionValue;
console.log(dataString);
} else {
questionValue = question + i;
dataString = dataString + "q" + i + "=" + questionValue + "&";
}
}
The loop will run 10 times, and each time it needs to add a part to the already existing dataString.
What it needs to do is make this string:
q1=(value of var question1)&q2=(value of var question2) and so forth.
The vars question1, question2, ... question10 all hold a number.
The first part works, it outputs q1=5 in the console log, however, after comes a random string. The output string (the total string) looks like:
q1=5&q2=NaN&q3=NaN&q4=NaN&q5=NaN&q6=NaN&q7=NaN&q8=NaN&q9=NaN&q10=question10
Does anybody know what I'm doing wrong?
You should use an array for this. There is no such thing as "variable variables" in JavaScript.
You can access a variable through a string containing the variables name by using this[variableName], but again, you shouldn't. You should use an array for this.
In your case, you would use questionValue = this["question" + i], but one more time: Don't do it. Use an array instead.
I'm not sure why you're using "question" + numberofquestions which will be 10 every time
Can anyone tell me why this is not working?
var fieldsValid = {
registerUserName: false,
registerEmail: false,
registerPassword: false,
registerConfirmPassword: false
};
function showState () {
var str = "<p>registerUserName: " + fieldsValid[registerEmail] + "</p>" ;
document.getElementById('showstate').innerHTML = str;
}
showState();
There is no output into my div.
Use quotes around the property name because otherwise, registerEmail is treated as a variable containing the property name, not a property name:
var str = "<p>registerUserName: " + fieldsValid['registerEmail'] + "</p>" ;
Or use the . syntax without quotes:
var str = "<p>registerUserName: " + fieldsValid.registerEmail + "</p>" ;
MDN Working With Objects is a good resource, relevant to this.
For future debugging, observe the console (F12) in your browser.
Let's say you have someObject.
someObject[johndoe] Returns the item in someObject that has johndoe's value (since here it is a variable) as index.