I am developing a webpage with fairly simple JavaScript. My entire JavaScript code is:
function showUnlockPopup(isViolated,instId,unlock_done,unlock_total,period,endDate){
alert(isViolated);
if(isViolated){
if(unlock_done < unlock_total){
showInfoPopup(instId,unlock_done,unlock_total,period);
} else {
showNoUnlocksPopup(instId,unlock_done,unlock_total,period,endDate);
}
} else {
showNotViolatedModal();
}
}
However, irrespective of the value of the 'isViolated' variable, my 'showInfoPopup' function is called.
I have checked the code for the method call too:
<button onClick="showUnlockPopup(isViolated)">Unlock</button>
The alert shows 'true' or 'false' values correctly, but the logic runs for only 'true' irrespective of the value.
I think I am missing something fairly basic here. I tried a lot, but of no use. Kindly help me with the same.
it is because isViolated is returned as a string. so unless the string is null it will be true if it has some contents. You should change your check to isViolated == 'true', make a new variable that is a boolean and assign it depending on isViolated or something third.
Related
I have this function:
function FizzBuzz(){
if(document.getElementById("textbox") == true){
Fizz();
} else {
Buzz();
}
}
The Fizz() and Buzz() functions place text in textboxes specified by document.getElementById in their own functions.
in it's current configuration, it should to my understanding execute Fizz() if ("textbox") is found on the webpage, otherwise Buzz() should be executed. This does not happen, and it will only execute one of them, no matter if ("textbox") is true or not.
The ("textbox") referenced above only exists on one of the two pages this is designed to work with.
Changing to !== true will invert the effect, the same with == false, as with the current behavior is expected. I have also tried to check for == null and !== null, this results in the same behavior.
I simply do not understand what I'm doing wrong here.
document.getElementById returns either null or element. So, comapring both with == or === with true or false will always return false.
You can directly put document.createElement inside if as a condition.
If you still face the issue, I suggest you to put a break point and see how the code is executed.
Sample code for better understanding - https://codepen.io/Yash__/pen/WNzgYvL?editors=1111
<h1 id="hai">hai</h1>
if(document.getElementById('hai')){
console.log("there");
}else{
console.log("not there")
}
I'm having some problems trying to use short circuiting on a web page I am making.
I am trying to use
document.webkitExitFullscreen() || document.mozCancelFullScreen() || document.exitFullScreen();
But it seems to stop on the first try, despite that I would have though it would continue after the first argument comes up as undefined.
If I simply type in
document.mozCancelFullScreen()
then it works fine
I was wondering if anyone could point me to what I'm doing wrong here
The screenshot is taken in firefox btw.
Thanks in advance
Your code is trying to call document.webkitExitFullscreen and if it returns a falsy value, call document.mozCancelFullScreen, etc.
But if document.webkitExitFullscreen itself is undefined you'll get an error trying to call it, and the code will stop running at that point.
Perhaps:
var exitFullScreen = document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen;
if (exitFullScreen) {
exitFullScreen.call(document); // Just `exitFullScreen();` may work as well
}
Or alternatively:
["webkitExitFullscreen", "mozCancelFullScreen", "exitFullScreen"].some(function(name) {
if (document[name]) {
document[name]();
return true;
}
});
...which avoids the whole "do I need call or not?" issue.
The problem is that you're already calling the function, so if it doesn't exist, you get an error.
You coudl try something like:
(document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen)();
Ok, I've got an issue I don't understand.
I have a boolean value which I test and if true I do something.
BUT javascript never go in it even if the var is true.
I try this :
if(isConfigD)
handleConfigurationD;
this :
if(isConfigD == true)
handleConfigurationD;
And this :
if(isConfigD === true)
handleConfigurationD;
But nothing work whereas isConfigD is always set on true :(
What am I missing ?
You condition works well, but if you call a function, then you need parenthesis for the call.
handleConfigurationD();
// ^^
handleConfigurationD is just an identifier. That statement is going to have one of two outcomes:
A ReferenceError
"Yup, that's a bit of data"
Presumably you have stored a function in it and want to call the function.
handleConfigurationD();
I am getting this error message in my Javascript.
Unable to get property 'style' of undefined or null reference
document.getElementById('BS2').style.display='block';
What is going on it that sometimes the Element ID is not showing. Is there a way to check if the element is there then do this else, go to the next line of code?
function showb() {
if(document.getElementById('BS1').style.display=='none') {
document.getElementById('BS1').style.display='block';
document.getElementById('BS2').style.display='block';
document.getElementById('BS3').style.display='block';
document.getElementById('BS4').style.display='block';
}
return false;
}
Sometimes the BS1 is showing, BS3 is Showing and BS4... etc.
document.getElementById() returns an object just like anything else. You can assign it to a variable and check the null-ness of the variable:
var bs1 = document.getElementById('BS1');
if (bs1) {
.. do stuff if bs1 is present
} else {
.. do stuff if bs1 is not there
}
AS pointy said in his comment, you should chekc whether there is an element with the desired ID before trying to read its properties. You could use a piece of code like this:
var element = document.getElementById('BS2'); // or any other ID
if (element) { // if no element is found, this evaluates as a logical false.
element.style.display='block';
}
Getting the element in a variable also has the advantage that your code becomes more readable. And since you use this multiple times, you could create a function to encapsulate that logic. Good luck!
Well if the property style hasn't been modified it means that it is just ("")
function showb() {
if(document.getElementById('BS1').style.display) {
document.getElementById('BS1').style.display='block';
document.getElementById('BS2').style.display='block';
document.getElementById('BS3').style.display='block';
document.getElementById('BS4').style.display='block';
}
return false;
}
it is going to be solved just like that because ("") is a falsy value
Thanks for taking time to review this question. I've been trying to fix a problem for one or two hours with no success...
I have a web page that sets a JavaScript variable based on the response from a function:
grade = getScore(questionAnswer, userAnswer, questionType);
(userAnswer is the user's answer to a question and is retrieved from a textarea)
Here is getScore:
function getScore(questionAnswer, userAnswer, questionType) {
switch(questionType) {
case 'multiplechoice':
return scoreMC(questionAnswer, userAnswer);
break;
case 'usertypesanswer':
return scoreTA(questionAnswer, userAnswer);
break;
default:
return 0
}
}
The functions for scoreMC and scoreTA have been tested thoroughly and work great. The issue is that if a user's answer is not formatted correctly, scoreMC or scoreTA will return false. Otherwise it returns the values score and msg. However, instead of getting a "false" value for "grade" when I set the value of the grade variable based on the getScore function, I get "undefined". (We have no problems when the user response validates properly.)
After setting "grade", I have tried to check if any part of it is undefined:
if(typeof(grade.score) !== undefined)
I do not understand why, but even when I see "undefined" in my Firebug console, grade.score passes this check...
Does anyone see what I am doing wrong? Thank you very much for your assistance. I have a lot to learn about JavaScript.
if(typeof(grade.score) !== undefined)
can be
if(grade.score && grade.score !== false) // if I understand your question
or
if(typeof(grade.score) !== "undefined")
typeof returns a string
If no return statement is used (or an empty return with no value), JavaScript returns undefined.
It is almost certain that one of your score functions (scoreMC, scoreTA, whose code you should have included in the question) does not return a value i.e.
return;
Or just reaches the end of the function code block without encountering a return.