If I run this code in Firebug everything goes fine and works:
var ingarray =$(".ingsparts");
$.each(ingarray ,function(n){
var ing = ingarray[n];
console.log($(ing).find('.name').val());
console.log($(ing).find('.value').val())
});
but if I run this, it doesn't work:
var ingarray =$(".ingsparts");
$.each(ingarray ,function(n){
var ing = ingarray[n];
var in = $(ing).find('.name').val();
var ms = $(ing).find('.value').val();
});
It seems that in is a reserved word; use another variable name.
in is a reserved word in Javascript (see here for more info), you will have to rename this variable.
Yeah, dont use in as variable name, but also, your each can be done more simply:
var ingarray = $(".ingsparts");
ingarray.each(function(){
var name = $(this).find('.name').val();
var value = $(this).find('.value').val();
...
});
The second example defines the in and ms variables inside the function. This means that they get function scope and are not usable outside of the function. So the variables are set, but never used and not accessed.
Related
Original content for context:
When ever I try to add var text0 = document.getELementById("text0"); to the following script, it breaks (it works without the text0 variable definition) how can I fix this?
Script:
function extend0 ()
{
var nav0 = document.getElementById("nav1");
var text0 = document.getELementById("text0");
{
nav0.style.paddingBottom = ("100px");
nav0.style.paddingLeft = ("100px");
nav0.style.paddingRight = ("100px");
text0.style.opacity = ("1");
}
}
Edit, after a few years:
Please be careful with your code and look out for syntaxical and spelling errors, this includes mis-casing, JavaScript is a case-sensitive language meaning it doesn't 'understand' if you use the wrong case. Ensure you look over your code for any errors, especially in new lines of code that you have just deployed, causing breakages.
Javascript method names are case-sensitive. You're calling getELementById, not getElementById (with a lower-case L).
Although this question is about datatables, it's not specifically about it.
I have datatables init stored in variables. The variable name varies as I have several datatables on the page. I am trying to collect the content of those variable by assembling the variable's name and I don't know how i can later use the 'string' I've assembled as a variable
For example:
var var_1_id_0 = $('.item1').datatable();
var var_1_id_1 = $('.item2').datatable();
var var_1_id_2 = $('.item3').datatable();
// later in the code.
var varname = 'var_1_id'+'_0';
// varname now holds the string 'var_1_id_0' which is the first variable.
My question is how can I use varname's string 'var_1_id_0' as the variable 'var_1_id_0'?
I hope that make sense.
Thanks
It is not possible. The closer solution is this one:
var datatable={};//A new Object.Arrays doesn't work property for this.
datatable['var_1_id_0'] = $('.item1').datatable();
datatable['var_1_id_1'] = $('.item2').datatable();
datatable['var_1_id_2'] = $('.item3').datatable();
// later in the code.
var varname = 'var_1_id'+'_0';
console.log(datatable[varname]);
Try it like this:
var varname = 'var_1_id'+'_0';
alert(window[varname]);
I am trying to set a javascript global var using jquery and dynamic variable names like this:
var home_phone_number // located outside of any functions
.
.
.
function setPhoneVars(phone){
// do stuff here to determine the correct prefix
thePrefix = 'home_phone_'
$(thePrefix + "number").val(phone.number);
}
When I do this, the value of home_phone_number is undefined.
But, when I set the phone number manually, like this:
home_phone_number = phone.number
the variable is set as expected.
Global variables are properties of the window object, so can be accessed as such:
window[thePrefix+'number'] = phone.number;
You can access global variables through window object, e.g.
var home_phone_number = "value";
function setPhoneVars(phone) {
var thePrefix = "home_phone_";
window[thePrefix + "number"] = phone.number;
}
Instead of having such many globals.. you can use a single object..
var globals = {
home_phone_number: 0 // located outside of any functions
}
function setPhoneVars(phone){
// do stuff here to determine the correct prefix
thePrefix = 'home_phone_'
globals[thePrefix + "number"] = phone.number;
}
I think your use of JQuery is not appropriate; .val() is intended to set the value of HTML elements, i.e. HTML objects in the DOM.
To simply set a dynamic JavaScript variable you could use eval() which is a JavaScript function which treats a string as executable code;
thePrefix = "home_phone_";
eval(thePrefix + "number = phone.number;");
I'm trying to add a variable within a new variable.
My first variable is:
var video5 = myObj.find('hosted-video-url').text(); (this returns a direct link to an mp4-file)
My second one should be something like:
var playvid5 = "playVideo('"video5"')";
Variable playvid5 should result "playVideo('http://link.to/video.mp4)')"
When I try to make variable playvid5 in the way I showed above, my whole code stops working and nothing is displayed. When I use var playvid5 = "playVideo('"+video5+"')";, the output is "playVideo('')", so that's not what I need either.
I'm trying to place the 2nd variable in this piece: ('Bekijk video')
In what way can I place the first variable in the second one?
Try to replace video5 string by its value.
var video5 = myObj.find('hosted-video-url').text();
var playvid5 = "playVideo('video5')";
playvid5 = playvid5.replace("video5", video5);
Why not just give the <a> tag an "id" value, drop it in the document, and then do:
$('#whatever').click(function() { playVideo( video5 ); });
Now, where you go to find the value, I don't think you've got the correct selector. Probably you need
var video5 = myObj.find('.hosted-video-url').text();
The "." before the string "hosted-video-url" is to select by class name. If "hosted-video-url" is an "id" and not a class, then you don't need to use .find(); you can select by "id" with $('#hosted-video-url').
Do you mean
var playvid5 = "playVideo('" + video5 + "')";
playvid5 will then be the string "playVideo('http://whatevervideo5is')
if video5 is blank then you will get "playVideo('')" so maybe that is the issue.
I have been trying for hours to fix this code, I can't see what's wrong:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal('+s_d+','+ddmmyy+')">'
The problem is in href="javascript ..."
s_d is a javascript variable defined as
var num = 2;
var s_d = "sname"+num;
var ddmmyy = "ddmmyy";
Basically I need to call a javascript function with different parameter each time.
Use a backslash like \'.
document.getElementById('detail'+num).innerHTML=
'<a class="dobpicker" href="javascript:NewCal(\''+s_d+'\',\''+ddmmyy+'\')">'
Since this is the value of a href attribute, HTML encode them:
document.getElementById('detail'+num).innerHTML='<a class="dobpicker" href="javascript:NewCal("'+s_d+'","'+ddmmyy+'")">'
Or better yet don't use the javascript: protocol:
[0,1,2,3,4,5].forEach(function(num) {
var s_r = "sname"+num;
var ddmmyy = "ddmmyy";
var aEl = document.createElement("a");
aEl.className = "dobpicker";
aEl.onclick = function() {
NewCal(s_d, ddmmyy);
}
document.getElementById('detail'+num).appendChild(aEl);
});
Your .innerHTML setting is using s_d, but your variable declaration has s_r.
EDIT: That was the first thing that jumped out at me. Having looked a bit closer and realised the values are strings, I think fixing the variable name together with adding some escaped quotation marks as in Daniel A. White's answer will do the trick.