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 1 year ago.
Improve this question
I have one webpage ,when loading the page it asks some questions if all the questions are correct only then it displays the body part otherwise the question will not allow next question or it should not displays the body part , please help me to fix this issue...
<!DOCTYPE html>
<html>
<head>
<title>Special-Wishes </title>
<script>
let q1=prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content
if(q1 == "John" || "JOHN" ){
let q2=prompt("what's your nick name...?");
if(q2=="blabla"){
alert("welcome to the page");
}
}
</script>
</head>
<body>
<h1>My body section</h1>
</body>
The reason why it is still asking the next question is due to your if statement logic
if(q1 == "John" || "JOHN" ) should be if(q1 == "John" || q1 == "JOHN" )
An even easier way of doing this would be if(q1.toUpperCase() == "JOHN")
In order to not display the body, you want to either remove it, or make it hidden. This can be done in a else block after your if statement
Remove: document.body.remove();
Hide: document.body.style.display = "none";
Use document.body.style.display = "none" when condition not matched
let q1 = prompt("what is your name...?"); //if the q1 answer is wrong it should not display the body content
if (q1 == "John") {
let q2 = prompt("what's your nick name...?");
if (q2 == "blabla") {
alert("welcome to the page");
} else {
document.body.style.display = "none"
}
} else {
document.body.style.display = "none"
}
<body>
<h1>My body section</h1>
</body>
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 4 years ago.
Improve this question
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
I cannot find the errors
it's a javascript code
<!Doctype html>
<html>
<title>JavaScript Tutorial</title>
<body>
<script language = "javascript">
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
if(cityToCheck === cleanCities[i]) {
alert("You are clean");
}
</script>
</body>
</html>
It says there is some problem in line 14
Where is your for loop for which you're an index i? If its removed intentionally then Tucson in your array has a space in the end. Use trim method.
Also language = "javascript" is wrong, its usually type="text/javascript"
The code should be like below:
enter code here
var cleanCities = ["Cheyenne ","Santa Fe ","Tucson ","Great Falls ","Honolulu"];
var cityToCheck = "Tucson";
for(var i=0; i<cleanCities.length; i++) {
if(cityToCheck === cleanCities[i].trim()){
alert('you are clean');
}
}
This question already has an answer here:
Why doesn't my equality comparison using = (a single equals) work correctly? [duplicate]
(1 answer)
Closed 5 years ago.
I'm trying to run this simple javascript code but doesn't get desired output. This code showing "You are pass" instead of "You are fail". Please tell where im wrong.
<html>
<head>
<title>
If else if and else use
</title>
</head>
<body>
<script>
function ifelseifelse() {
var marks=32;
if (marks>33){
alert("You are Pass");
}
else if(marks=33)
{
alert("You are pass");
}
else{
alert("You are fail");
}
}
</script>
<button type = "button" onclick="ifelseifelse()" >If else-if if</button>
</body>
</html>
You are using
else if(marks=33)
instead of
else if(marks==33)
You are assigning 33 to marks instead of comparing it
Use == in the else-if to compare your marks with 33
it's getting hung up at the second statement.
if(marks=33) should be if(marks === 33)
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'm still new in JavaScript. I am trying to make a program that contains 2 buttons and once a button is clicked, it creates a random number.
The problem is that when I'm trying to compare them, it is not showing which one is bigger. First I thought the problem is that the variables aren't global but it didn't change anything.
Can someone help me find the problem please?
Here is the JavaScript code:
var par1 = document.getElementById("para1");
var par2 = document.getElementById("para2");
var winner = document.getElementById("win");
function button1() {
num1 = Math.floor(Math.random() * 7);
par1.innerHTML = num1;
}
function button2() {
num2 = Math.floor(Math.random() * 7);
par2.innerHTML = num2;
}
if (num1 > num2) {
winner.innerHTML = "the winner is player 1";
} else {
winner.innerHTML = "the winner is player 2";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dicestimulator</title>
<link rel="stylesheet" href="dicestimulator.css">
</head>
<body>
<div class="container">
<div>
<h1>Player1</h1>
<button type="button" name="button1" onclick="button1()" id="but1">roll
dice</button>
<p id="para1">Click the button to see what you get</p>
</div>
<div>
<h1>Player2</h1>
<button type="button" name="button2" id="but2" onclick="button2()">roll
dice</button>
<p id="para2">Click the button to see what you get</p>
</div>
<p id="win">let's see who wins!!!</p>
<script src="dicestimulator.js"></script>
</div>
</body>
</html>
if(num1>num2){
winner.innerHTML="the winner is player 1";
}else{
winner.innerHTML="the winner is player 2";
}
You are not calling the above block no where in your HTML code.
My solution for you make a third button that calls the function getwinner
function getWinner() {
if(par1.val>par2.val){
winner.innerHTML="the winner is player 1";
} else{
winner.innerHTML="the winner is player 2";
}
}
Please note that you cannot call local variables created in functions outside those functions. num1 and num2 cease to exist after the scope of the function that created them.
The section of code:
if(num1>num2){
winner.innerHTML="the winner is player 1";
}else{
winner.innerHTML="the winner is player 2";
}
… is not inside a function.
It runs when the script is initially loaded.
Later on, you call the button1 and button2 functions. These change the values of num1.
You never compare the values again.
If you want to compare them when the button1 and button2 functions run, then the code needs to be called when those functions are.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I'm trying to make a script that adds a link to a div in the onload and for some reason the content (images) is displayed as written html text. Why is that and how can I correct it?
<!DOCTYPE html>
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
function AffiliateLink() {
var boxes = document.getElementsByClassName('AffiliateLink'),
i = boxes.length;
while (i--) {
var link = document.createElement('a');
var content = document.createTextNode(boxes[i].innerHTML);
link.setAttribute('href', boxes[i].getAttribute('title'));
link.appendChild(content);
boxes[i].innerHTML = '';
boxes[i].appendChild(link);
}
}
</script>
</head>
<body onload="AffiliateLink()">
<div class="AffiliateLink" title="http://www.google.com">Google</div>
<div class="AffiliateLink" title="http://www.apple.com"><img src="http://files.softicons.com/download/application-icons/black-icons-by-mike-demetriou/png/128x128/App%20apple%20logo.png"></a></div>
</body>
The document.createTextNode forces the HTML to be text. Just copy one innerHTML to the other.
http://jsfiddle.net/86fWM/1/
while (i--) {
var link = document.createElement('a');
link.setAttribute('href', boxes[i].getAttribute('title'));
link.innerHTML = boxes[i].innerHTML;
boxes[i].innerHTML = '';
boxes[i].appendChild(link);
}
Because that's explicitly what a textNode is that you're creating, it explicitly handles everything you give it as text and does not interpret the HTML. If you want to set HTML content, use the innerHTML attribute:
link.innerHTML = boxes[i].innerHTML;
It's debatable whether you may want to simply clone or move the boxes[i] node instead though instead of assigning one innerHTML to another.
Because you are creating a text node containing html tags, so they are treated as text and not html.
You can easily fix that by replacing
var link = document.createElement('a');
var content = document.createTextNode(boxes[i].innerHTML);
link.setAttribute('href', boxes[i].getAttribute('title'));
link.appendChild(content);
by
var link = document.createElement('a');
link.setAttribute('href', boxes[i].getAttribute('title'));
link.innerHTML = boxes[i].innerHTML;
More then one way to the target. the "create text node" made text node. replacing it with innerHTML will make the next code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function AffiliateLink() {
var boxes = document.getElementsByClassName('AffiliateLink'),
i = boxes.length;
while (i--) {
var a = document.createElement('a');
var content = boxes[i].innerHTML;
a.href = boxes[i].title;
a.innerHTML = content;
boxes[i].innerHTML = '';
console.log (i);
boxes[i].appendChild(a);
}
}
</script>
</head>
<body onload="AffiliateLink()">
<div class="AffiliateLink" title="http://www.google.com">Google</div>
<div class="AffiliateLink" title="http://www.apple.com"><img src="http://files.softicons.com/download/application-icons/black-icons-by-mike-demetriou/png/128x128/App%20apple%20logo.png"></a></div>
</body>
</html>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a code in HTML using Spring notation.
The code is:
<c:if test="${testCSV==true}">
<img src="images/icons/edit.png" width="25" height="25" />
</c:if>
I want to check this condition in jQuery, So the condition checks dynamically.
please help me.
Add this code in html where you want to edit button to show
<a id="edit" href="#" onclick="editCSV(${command.id})" rel="tooltip-top">
<img src = "images/icons/edit.png" width = "25" height = "25" />
</a>
js
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
pass true
<script type="text/javascript">
var test =pagetype('<%=${testCSV}%>');
if (test == true) {
$('#edit').show();
} else {
$('#edit').hide();
}
</script>
You can use this:
<script type="text/javascript">
var x = ${testCSV};
// some other code
</script>
The browser will see it as:
<script type="text/javascript">
var x = true; // or false
// some other code
</script>
Then, do whatever you like with it.