Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to determine whether the checkbox is checked or not, but when I push the checkbox, the javascript displays:
false
true
And when I push it again it changes to
true
false
And then it continues as I push further...
How can I only display one of those?
Here is my javascript code:
function selected() {
const bg = document.getElementById("myCheck").checked;
console.log(bg);
if (bg == 'true') {
document.getElementById("changeBG").style.backgroundColor = 'red';
} else {
document.getElementById("changeBG").style.backgroundColor = 'green';
}
}
This is incorrect:
if (bg == 'true') {
The checked property gives you a boolean, not a string. true is a string.
Just use:
if (bg) {
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I would like to turn this if statement into a Switch but Im not sure how to go about doing it. Thanks for your help.
if ((VS_WRNG !== "") && ((VS_WRNG == "DSC") || (VS_WRNG == "DSQ")) && (VS_BTYP === "")) {
setValue('BOB_TYPE',"LIN");
VS_BTYP = "LIN";
}
If you must, this would be one possible formulation:
function x() {
switch (VS_WRNG) {
case "DSC":
case "DSQ":
if (VS_BTYP === "") {
setValue('BOB_TYPE', "LIN");
VS_BTYP = "LIN";
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I tried making a tool that calculates battle out comes but this part of the code always displays: "DRAW!"
function battle()
{
var rawpower = document.getElementById('rawpower').value;
var rawpoweropp = document.getElementById('rawpoweropp').value;
if(rawpower > rawpoweropp){
alert("You won!");
} else if(rawpower < rawpoweropp){
alert("You lose!");
} else{
alert("Draw!");
}
}
The element with id="rawpower" is a paragraph tag <p>. These elements do not have values. So document.getElementById('rawpower').value returns undefined, and same for the other line. undefined is not less than undefined, nor is it greater than undefined, so you're going into the third case.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I wrote a for loop in JavaScript which is simple and straight forward. But, it is skipping the 2nd index. I don't understand why?
Below is my code snippet :
if($scope.usersChoice.length == $scope.correctAnswers.length){
for(var p=0;p<$scope.usersChoice.length;p++) {
if($scope.usersChoice[p] == $scope.correctAnswers[p]){
$scope.score++;
}
}
}
Here the length is 10.
How do you know it is skipping the 2nd index? Because it should'nt.
Can you show us your arrays ?
By the way, you can use forEach instead of for loop in this case, since you don't seem to need to break your loop:
if($scope.usersChoice.length == $scope.correctAnswers.length){
$scope.usersChoics.forEach($choice,$index=>{
if($choice === $scope.correctAnswers[$index]) $scope.score++;
});
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Given some html:
<select id="this">
<option value="a"></option>
<option value="b"></option>
</select>
If your JS does different things depending on which option was selected, I'm wondering, how "strict" are you in the comparison? I.e., how paranoid are you? So there's a spectrum of options:
// OPTION A - not very paranoid
select_value = $("#this").val()
if (select_value == "a") {
...
} else {
...
}
// OPTION B - more paranoid, strict ===, but also has another else jus tin case
select_value = $("#this").val()
if (select_value === "a") {
...
} else if (select_value === "b") {
...
} else {
// some kind of exception handling, e.g., an error alert pops up, in case somehow user sneaks in another value...
}
Just asking because I realized I constantly default toward the most paranoid solution ever for everything, which is likely unhealthy for me and my code.
In the question's example there's no need for paranoia. == will sufice.
=== is used when you want to check equality based on value AND type of the var.
== allows for implicit conversion of the compared vars.
So '1' == 1 is true, '1' === 1 is false.
As you're just checking the select's value there's no need for ===
P.S.: Are you a Black Sabbath fan or something ? if yes leave the paranoias to Ozzy. He's good with them. :D
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Well, the title of question look like easy, and the solution is:
$('.weatherCity').each(function () {
if ($(this).text() == 'LONDON') {
$(this).text('London Weather');
}
});
but I working on Yahoo! Weather Plugin and want to change city name, it's not iframe and we allow to change style and etc.. but look like it won't let change any text in this plugin. I thought browser read my code then yahoo get the weathers then I used promise().done() but nothing changed.
jsFiddle
Thanks in advance
-jiff
As a work around for this problem, you can use like this,
var interval = setInterval(function() {
if ($('.weatherCity').length) {
$('.weatherCity').each(function() {
if ($(this).text().trim() == 'London') {
$(this).text('London Weather');
}
});
clearInterval(interval);
}
}, 100);
Fiddle
Constantly checks for the element using setInterval and stop checking once it is found.