jQuery global variable, not working [duplicate] - javascript

This question already has answers here:
What does $(function() {} ); do?
(6 answers)
Closed 4 years ago.
Why is this simple global variable not working inside a function
var test_1 = "None";
$(function(){
console.log(test_1)
})
I am getting the result Undefined

Check, whether you have jquery or not. I have created a jsfiddle (https://jsfiddle.net/2kudvsr0/) for your code and its printing None (desired output).
Just make sure you are executing your code once jQuery is imported successfully.

Related

i cant pass a parameter to a function using javascript [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Closed 2 years ago.
i am trying to add a set time out function like this and it does not work
setTimeout(myfunction(parameter),200)
meanwhile the code below work but it does not get the job done cause i cant pass a parameter.can anyone explain why ?
setTimeout(myfunction,200)
Try
setTimeout(function() {
myfunction(parameter);
}, 200)
You can also use
setTimeout(myfunction.bind(null, parameter), 200);

Transform alert to console.log [duplicate]

This question already has answers here:
JavaScript: Overriding alert()
(12 answers)
Closed 4 years ago.
Is it possible to change the alert() function to log the message instead of showing up a popup?
Some outside team members does like the use the alert function, but we don't want it because they aren't removing them.
So I want to add some code in our angular application that logs the message in the console instead of popping up.
So, I want some code which don't require to change all the calls to alert() but a kind of hook to that function which transforms it to console.log()
Try this
window.alert = window.console.log
window.alert('Hello from console.log')

How to get the self-DOM object in callback [duplicate]

This question already has answers here:
jQuery Button Click 'this'
(3 answers)
Closed 5 years ago.
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
this.addClass('btn-default'); it didn't works.
I would like to get self-dom object(in this case $("#subPanel") itself) from inside the call back.
It might be easy problem, so I try to googled around.
However I couldn't get straight answer.
could you help me ??
Inspect this and you will see it's not a jquery object but a DOM element which does not have an addClass method. Try:
$("#subPanel").click(function() {
$("#subPanel").removeClass('btn-success');// it works
$(this).addClass('btn-default')
})
Example: https://jsfiddle.net/14s0h3dr/

Getting 'cant read if of null' [duplicate]

This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 7 years ago.
Here is html:
<div class="nama" id="xxx"></div>
And javascript :
var div = document.getElementById('xxx');
alert(div.id)
Why i am getting mistake?Looks like fine for me
The element was not found.
Most probably because this code was ran before that part of the DOM was ready.
Try placing your code below where the HTML is defined.
window.onload = function() {
var div = document.getElementById('xxx');
alert(div.id)
};

Result of syntax in JS [duplicate]

This question already has answers here:
How is this valid javascript, and what purpose does it serve?
(2 answers)
Closed 7 years ago.
I'm switching from C++ to JS.
//if ( condition )
{
instruction1;
instruction2;
}
In C/C++ commenting if before the block would simply execute the block unconditionally.
Could I use it the same way in JS? Or would it create an unnamed object which is never used?
A very quick way of testing:
{
alert("foo");
}
http://jsfiddle.net/4obksb0s/
Yes - it appears to run fine.

Categories