let value = 'italic';
let varname = "fontSizeTitle";
eval(varname + "=" + value);
console.log(fontSizeTitle)
I have a big project in which i use eval() to assign dynamic variables to dynamic strings. The problem here is it throws an error saying italic is not defined but when if i want to use value as a string but again being dynamic? how do i tell eval() that i want the value to be dynamic. Sorry for not posting my project code but its the exact scenario in here too.
Is italic a string literal as opposed to a variable name? If so, you must surround it with quotes in order to set it.
Your current eval statement does this:
fontSizeTitle = italic
This is possibly what it should be:
fontSizeTitle = 'italic'
The following code snippet will show this working:
let value = 'italic';
let varname = "fontSizeTitle";
let statement = varname + "='" + value + "'";
console.log(statement);
eval(statement );
console.log(fontSizeTitle)
I've added the statement itself to the console log so you can see what is actually being executed.
Related
I have a var in script which has data like . But when i add this to another variable its not working.
var x = '32"';
onclick="javascript:selectSize(' + "'" + x + "'" + ');"
I want
onclick="javascript:selectSize('32"');"
But it becomes
onclick="javascript:selectSize('32"');""=""
i don't know whats happening
onclick="javascript:selectSize('32"');"
^ ^
The HTML parser will parse the attribute value before passing the value of it to the JS engine for execution.
You are using a " to delimit the attribute value.
The second " ends the attribute value.
If you want to use " as data in an attribute value delimited with " then you must express it as an entity (e.g. ").
var html_x = x.replace(/"/g, """);
Escaping becomes very painful when you start generating nested languages.
You have JavaScript embedded in HTML embedded in JavaScript.
Avoid mashing strings together to construct your DOM. Use DOM methods directly instead.
var x = '32"';
var button = document.createElement("button");
button.addEventListener("click", function (event) {
selectSize(x);
});
from comment, Make use of encode/decode URI Component as follows
var a=encodeURIComponent('abc"');
console.log(a);
console.log(decodeURIComponent(a));
I'm looking for an easy way to assign to a variable depending on the value of another variable.
device.slot2_clipList[clipNumber] = singleClipDetails;
what I'm trying to do is: replace the "2" with another variable, so that i can run the same operation while just changing the
var slotNumber, and write to the corresponding variable.
i tried
device.slot + device.slotNumber + _clipList[clipNumber]
but (obviously?), this doesn't work.
How can this be done? (Maybe I named the Question incorrectly, but that was the closest I could think of.)
Thanks
This is what bracket notation is for
var i = 2;
device['slot' + i + '_clipList'][clipNumber] = singleClipDetails;
device['slotNumber' + _clipList[clipNumber] ]
Explanation:
foo.bar in javascript is identical (even in performance) to foo['bar']. So any object property name can be built up from strings.
I came across a piece of code written in below format in a .js file. I am wondering what would it actually do. Just trying to understanding the purpose of this statement.
var upload_file_count_text = +filenames.length + +upFiles;
The + prefix on the variable names is the 'unary' operator. In JavaScript it is in effect shorthand for Number(). The code is changing both filenames.length and upFiles to ints from string so their values can be added instead of concatenated.
The code is equivalent to:
var upload_file_count_text = Number(filenames.length) + Number(upFiles);
In javascript prefix + is use to convert string to Int
var upload_file_count_text = +filenames.length + +upFiles;
Above code first +filenames.length which simply convert it into int type
http://jsfiddle.net/zzTsc/
I have a JSON that holds some values which get concatenated into a string but when I change the value of the JSON the string doesn't take the name values. How can I make the string take the new values without re-declaring the same string?
See I could easily put string = name.first + "<br />"+name.last; right below where I change the JSON value but then when I wanna edit the format of that string I'll have to change it twice.
That's not how variables work. You have to set the value of string again for it to update.
Here's an improved version so you don't have to change it twice if you want to edit something:
function generateString(name) {
return name.first + "<br />"+name.last;
}
var string = generateString(name);
​Demo
That's not going to work as you describe it, but you could declare a function which returns the appropriate string.
So instead of
string = name.first + "<br />" + name.last;
You'd have:
var stringfunction = function() {return name.first + "<br />" + name.last;}
and when you wanted to use it you'd call it:
alert(string); //old
alert(stringfunction()); //new
EDIT: I just realized you're talking about changing the format (presumably at runtime). There are a number of ways to use string formats in javascript, meaning that you could have a "format" variable used inside the stringfunction, which you could change at runtime to modify how the resulting string is formatted. It's been too long since I used one of those tools though, so I'll leave it to someone else to explain how to do string formatting in javascript.
This is because your string is a basic type and therefore holds the actual data instead of a pointer. When you change the data you think should change the string, it only changes the data. This can be seen by just re-issuing the code used to construct the string
$('#test').empty().append(name.first + "<br />"+name.last);
http://jsfiddle.net/zzTsc/4/
Is there any way to access split values without putting them into separate value?
var content = "some|content|of";
var temp = content.split("|");
var iwilluseit = "something" + temp[1] + temp[2]
How to do this w/o temp variable ?? (inline in setting of iwilluseit var)
It's incredibly inefficient, but you could call split multiple times:
var iwilluseit = 'something' + content.split('|')[1] + content.split('|')[2];
There's also the slice() + join() option:
var iwilluseit = 'something' + content.split('|').slice(1,2).join('');
Really, though, just creating the temp variable is the best way to go.
content.split("|").slice(1,3).join("")
No, you need to assign the result of Array.split() to an intermediate variable before it can be used, unless you don't mind the performance hit of calling Array.split() for each value you want to grab.
You could patch String.protoype to add a method that would take an array of strings and substitute it into a string.
How about:
var iwilluseit = "something" + content.split("|").slice(1,3).join(""));
You can also do:
var iwilluseit = "something" + content.substr( content.indexOf( "|" ) ).split("|").join("");
Of course, this will only work if you are simply trying to remove the first value.
More importantly:
Why do you need it to be in line?
If the purpose of not assigning it to a variable is to be able to do this in a context
where you can only have one Javascript expression, you could also use a closure, and
assign it to the variable in it:
(function() { var temp = content.split("|"); return "something" + temp[1] + temp[2]; })()
Which would be usable in an expression context, and not have the performance hit.