How do I change a variable in a jQuery function?
Do I have to override the entire function?
Below is a function found in jquery.dataTable.js
How do I add a "," to the sValidChars variable.
$.extend(DataTable.ext.aTypes, [
function (sData) {
....
var sValidChars = "0123456789.";
....
}
]
You will have to override the entire function/change the code in the actual file.
It is because sValidChars is a local variable to the anonymous function, so it cannot be accessed outside the function scope.
Related
I'm kind a new in Javascript and I have this situation:
I have a separated javascript file and I created a javascript object in this way:
$(document).ready(function () {
var formasPagamento = {
Cartao: 0,
Crediario: 1,
Vale: 2
};
});
In this same file, I have a function and I want to use this object formasPagamento, but when I try to use it, I get the error that formasPagamento is undefined.
Ex:
function CarregarDetalhesPlanoPagamento(idPosDocPagamento) {
if(idPosDocPagamento == formasPagamento.Cartao){ //undefined here
//do something
}
}
What's the proper way to initializa a "global" variable that can be used in another function?
Don't wrap var formasPagamento inside of $(document).ready. That is placing it inside of a child scope of global, making it inaccessible from your function. $(document).ready is to detect when the DOM is ready to be manipulated and there's no reason to postpone declaring that value.
What's the proper way to initializa a "global" variable that can be
used in another function?
window.formasPagamento = {
Cartao: 0,
Crediario: 1,
Vale: 2
};
In your example, you're declaring formasPagamento inside the document.ready jQuery wrapper, so formasPagamento can only be accessed from within that function.
This is known as scope.
To be able to access formasPagamento globally, you can declare it before your $(document).ready.. like so:
var formasPagamento = {
Cartao: 0,
Crediario: 1,
Vale: 2
};
$(document).ready(function () {
..page ready logic..
});
Now when you use your function, it'll be able to see formasPagamento
This article explains it quite well w3School
But basically the variable formasPagamento is only accessible inside the function scope of the document ready function.
$(document).ready(function () {
var formasPagamento {};
// CAN access here!
}
// CANNOT access here!
My code is...
<script type="text/javascript">
function changeval() {
$total = parseInt($("#small").val()) + parseInt($("#medium").val());
}
</script>
How can I pass '$total' to another js file, say main.js.
Call changeval function from another file, save/use returned value.
function changeval() {
return parseInt($("#small").val()) + parseInt($("#medium").val());
}
Or have one global variable save value inside the same variable and access it from another file.
Make sure that $total is assigned to the global scope and make sure the script where you want to use it is below the script where you assign it. Be careful about polluting the global scope too much because it can make code that is very tricky to debug and can cause strange errors if you overwrite a native global variable.
listofLOBs: function(data) { //passing data
var returnLOB = [];
console.log(data);
for (var i1r= 0; i1r < data.length; i1r++) {
var lob = new LOBslist();
lob.name = data[i1r].name;
lob.id = data[i1r].id;
returnLOB.push(lob); // storing in returnLOB
}
console.log(returnLOB[1].name);
$('#mainWrapper').find('select[name="lob-select"] option:selected').text(returnLOB[1].id); //using the returnLOB's object data to populate dropdown
return returnLOB;
}
In the above function i am storing objects in the returnLOB array and using it inside the function itself to populate a dropdown.
But say this is file1.js, i would like to access returnLOB's values from file2.js How can i do that? Thanks in advance.
Note: i have declared returnLOB[] globally too.(outside the above function)
To do what you want, you just need to remove var from your function so that you're not creating a local variable that shadows the global variable you've created in another file.
listofLOBs: function(data) {
// Here you're setting a global instead of a local
returnLOB = [];
You probably know this is a bad idea, and it would be better if you restructure your code so that you don't need a global
A simple solution is to have all code that needs the variable in one place with a self-executing function so your global is only exposed to the code that needs it
(function(){
var returnLob;
... contents of file 1, which write to returnLob
... contents of file 1, which read returnLob
})()
As long as you include file1.js before file2.js, it will work. Take jQuery as an example - or any JS library for that matter - if you include its source file before your code, you can use functions within it.
You want to include the files in the above order, then you can just call the function (listofLOBs()) in file 2.
Additionally, your variable declaration within the function is incorrect.
var returnLOB = []; // You're declaring a global.
should be
returnLOB = []; // because the variable is already declared - you just want a local copy.
Hope this helps.
You should be able to access any globals between files. In your example you defined returnLOB inside of a function so that won't be accessible but the one you claimed to have defined globally will be accessible by both files.
This also has been asked before so check out:
Javascript: Scope of a Variable across different Javascript files
I wonder if somebody could please help me understand something that seems odd with JS?
The below code works. The function inlineEditEvent.init() is called, and then t.copy() is called correctly (where var t = this;).
However, if I was to replace that with this.copy(), I get the error this.copy is not a function.
What's the difference here? Why does the below work, but not the way as described in the last paragraph? Thanks.
jQuery(function($){
$(document).ready(function(){inlineEditEvent.init();});
inlineEditEvent = {
init : function(){
var t = this;
/** Copy the row on click */
$('#the-list').on('click', '.row-actions a.single-copy', function(){
return t.copy();
});
}, // init
copy : function(){
// Do stuff here
}
} // inlineEditEvent
});
You're setting t as a context variable of this (of your init function). Once inside your click handler, this is now referring to the click handler, no longer the init function. Therefore, this.copy() is not a function.
this refers to this within the functions scope. That's why you need to set a self variable, so it's accessible within the scope of the function. Considering you're using jQuery, you could use $.proxy:
$.proxy(function(){
return this.copy();
},this)
t.copy(); appears in a different function to var t = this;. The value of this changes inside each function.
When you say var t= this; it refers to what this meant in that context. Later on when you are trying to refer to this, it is referring to a.single-copy instead since that is the new context it is in.
I'm trying to access a global variable in testGlob1, however I'm not able to do so:
var displayVar;
function globVariable(){
displayVar="2";
}
function testGlob1(){
alert(displayVar);
}
You aren't showing how your variable is being assigned a value. Is globVariable() being called on load of the document? Im sure this would work:
var displayVar=2;
function testGlob1(){
alert(displayVar);
}