I have a string that equals 'Res' described by:
ResEmp = drpdwn.id.substring(3,6)
If I wanted the following code:
parseFloat(AppResYrs.value)
to get 'Res' from the ResEmp variable, what syntax would I use?
I've tried parseFloat('App' + ResEmp + 'Yrs.value') and parseFloat(('App' + ResEmp + 'Yrs').value) and they don't work. Thank you!
Edit: The following code: alert(ResEmp); alert(parseFloat(AppResYrs.value)); alert(parseFloat('App' + ResEmp + 'Yrs').value); returns 'Res', '0' and 'undefined'. The first two are correct. I want the last one to return 0 also, because I want it to mean the same thing as the second.
Assuming it's the ID of a DOM element, you'd do;
parseFloat(document.getElementById("App" + ResEmp + "Yrs").value);
If it's a global object with value attribute, you could cheat and do;
parseFloat(window["App" + ResEmp + "Yrs"].value));
Otherwise you can't access it.
Related
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.
I am doing a simple assignment for one of my modules and I've come into this problem for the 3rd or 4th time. My problem is that as my code shows below when I put the .innerHTML attribute on the first line it does not work at all and there is no error in the console window. When I place the it after the info.innerHTML on the second line it does work. I have asked a tutor and he is stuck on why this is happening
var info = document.getElementById("pOutput").innerHTML;
info = "Sum: " + sum + "<br>Average: " + avg;
var info = document.getElementById("pOutput");
info.innerHTML = "Sum: " + sum + "<br>Average: " + avg;
The second variation that you've included is correct but needs getElementById() instead of GetElementById().
The top lines are incorrect because you are overriding the variable info with your string, not writing it into innerHTML.
When you set info to innerHTML, it's getting the value. You need to set info to document.GetElementById("pOutput"), and then set info.innerHTML:
var info = document.getElementById("pOutput");
info.innerHTML = "Sum: " + sum + "<br>Average: " + avg;
The reason why this is happening is because in the first bit of code, you capture the value of the innerHTML property of the element. For example, if your element has <div></div> inside it, the value of info will be "<div></div>". If you modified the innerHTML of the element after, the value of info would still be <div></div>. On the contrary, in the second bit of code, what you capture is a reference to the element. Therefore you can modify its properties and it will reflect on the element itself. The reason why this happens is explained here but to make it simple, some types (like strings and numbers) are copied as values and some other types (like Objects) are copied as references.
This is fixed! Already have a correct answer.
("<div class='script' onclick='" + 'open_script(' + data + ', ' + data_name_found + ')' + "'>" + data_name_found + "</div> ")
What is wrong with the above code? I am making a project, and am assigning a variable that information to store inside of a div. The variable data is all the data, and data_name_found is the specified name is all the names with other info inside the data variable. The open_script function is supposed to open a div with information about the given script. Sort of like an edit menu, if at all possible, I would prefer not to give out any more code from my project.
Thanks for the help in advance!
EDIT--> The problem is that it won't even trigger the other function. I have been working on this problem for quite a while and can't find out why. May be cause I'm tired, sorry if it's a silly mistake!
It's hard to tell without more code, but the offending piece of code seems to be:
+ 'open_script(' + data + ', ' + data_name_found + ')'
If data and data_name_found are strings, they're being outputted into the HTML without quotation marks. Assuming that's the problem, this should fix it:
+ 'open_script("' + data + '", "' + data_name_found + '")'
I would like to add a variable in an href so that it looks like, or behaves like, this:
var user = "YourName";
$("#main-container .profile-link[href='/home/user/' + player + '/']")
I'm hoping to be able to select all the users that I have in that variable.
Use proper string enclosures
$('#main-container .profile-link[href="/home/user/' + player + '/"]')
In your case since the string literal is started using " you need to use the same to close the string before the variable is concatenated like "#main-container .profile-link[href='/home/user/" + player + "/']"
I have got this link:
Visit imLive.com
I want to use this code to add/change different url parameters:
$("a.sitelink_external.imlive").each(function(){
$params=getUrlVars(document.URL);
var promocode_addition='';
if('INFO'==$params['ref']){
promocode_addition='LCI';
}
$(this).attr("href", 'http://im.com/wmaster.ashx?WID=124904080515&cbname=limdeaive&LinkID=701&queryid=138&promocode=LCDIMLRV" + i + promocode_addition+"&"FRefID=" + FRefID + "&FRefP=" + FRefP + "&FRefQS=" + FRefQS');
});
The problem is that that jquery code doesnt work..I tried to move it to document ready..but it doesnt work there too..
The thing that jumps out at me is that you're mixing your double and single quotes on this line:
$(this).attr("href", 'http://im.com/wmaster.ashx?WID=124904080515&cbname=limdeaive&LinkID=701&queryid=138&promocode=LCDIMLRV" + i + promocode_addition+"&FRefID=" + FRefID + "&FRefP=" + FRefP + "&FRefQS=" + FRefQS');
Try changing them all to double quotes, and remove the extra " from after the ampersand in "&"FRefID=" - like this:
$(this).attr("href", "http://im.com/wmaster.ashx?WID=124904080515&cbname=limdeaive&LinkID=701&queryid=138&promocode=LCDIMLRV" + i + promocode_addition+"&FRefID=" + FRefID + "&FRefP=" + FRefP + "&FRefQS=" + FRefQS);
The way you had it was a single string containing stuff that looked like code. The way I've changed it is several strings and variables being concatenated together... (Note the difference with StackOverflow's syntax highlighting.)
Note also that the following code:
$params=getUrlVars(document.URL);
var promocode_addition='';
if('INFO'==$params['ref']){
promocode_addition='LCI';
}
...can be moved to before the .each() loop, since it operates only on the document and thus will produce the same results on every iteration.
(Of course there could be other problems since you reference several variables that aren't shown.)