How to make button appear when dropdown option is clicked? - javascript

document.getElementById("SameDifferent").addEventListener('click',function () {
start(game.SameDifferent);
document.getElementById("instructions").innerHTML = "Press S for 'same' and D for 'different'";
} );
So right now, when I click toggle the function to start the game, I have instructions appear by changing the innerHTML in my html file. I want two buttons to pop up instead, however, that say "Same" and "Different". I'm almost a complete beginner at HTML/Javascript, so not sure how to do this. I can make the button appear constantly, but I am confused on how to toggle it in the js file.
Any help would be appreciated. Thanks!

You can try to set the button to hidden like below:
document.getElementById("button1").style.display = "none";
To show:
document.getElementById("button1").style.display = "";

Hi consider this HTML:
<button onclick="toggle()">Click Me</button>
<div id="tggle">
This is my DIV element.
</div>
It displays a button (toogle), that shows the div with ID tggle, here's the javascript to toggle it:
function toggle() {
var x = document.getElementById("tggle");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

Related

First click not working in Show hide section in WordPress Elementor on a button/text click

I am hiding a form through button click and javascript. It works fine, but I have a problem on the first click.
When I get to the page, if I make the first click on the button, it doesn't work, but when I click on the second one, it starts working with just one click hide / view.
If I refresh the page, the first click doesn't work again.
<button class="myclass" onclick="myFunction()">
<style>my style</style>
<script>
function myFunction() {
let x = document.getElementById("noform");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}

Unable to hide div using js, but I'm able to show div with js. Seems like "else if" isn't working. Could someone look over this?

I have a div that I can show with js, but I can't seem to close. Using if .... else if...
For example:
<a id="downgrade" onclick="showDowngradeDiv()" href="javascript:void(0)"><h1>Downgrade ▼</h1></a>
<div id="downgrade-text">
<h2>text</h2>
<p>A paragraph of text</p>
</div>
function showDowngradeDiv() {
const DowngradeDiv = document.getElementById("downgrade-text");
if (DowngradeDiv.style.display = "none") {
DowngradeDiv.style.display = "block";
}
else if (DowngradeDiv.style.display = "block") {
DowngradeDiv.style.display = "none";
alert("test");
}
}
#downgrade-text {
display: none;
}
If anyone has any idea, let me know because I don't know what I'm doing wrong here right now.
You are assigning the display property at the if/else. Please use the double equal sign operator.
if (DowngradeDiv.style.display = "none")
if (DowngradeDiv.style.display == "none")
Funnily enough, just messed around with js and ended up doing this and somehow it's working?
function showDowngradeDiv() {
const DowngradeDiv = document.getElementById("downgrade-text");
if (DowngradeDiv.style.display == "block") {
DowngradeDiv.style.display = "none";
}
else {
DowngradeDiv.style.display = "block";
DowngradeDiv.scrollIntoView({behavior: "smooth"});
}
}
but if I were to switch the statements around and say if Downgrade.style.display == "none" then change it to block and scroll to it else set display to none. That wouldn't work for some reason, but flipping it seems to have gotten me what I wanted functionally.

JavaScript addEventListener 'click''

I struggling with very basic thing
I am trying to make this side-nav appear and disappear with JavaScript. The code below only activates the nav bar but doesn't deactivate it. Tried all options but I don't know how to call function to close nav-bar.
Thank you
HTML
<div id="mySidenav" class="sidenav">
×
About
Services
Clients
Contact
</div>
<!-- Use any element to open the sidenav -->
<span id="MyElement">×</span>
JS
<script type="text/javascript">
function changeClass() {
document.getElementById("mySidenav").style.width = "250px";
}
window.onload = function() {
document.getElementById("MyElement").addEventListener('click',changeClass);
}
</script>
</body>
</html>
You can't have to objects with the same ID (use name instead?)
You have a typo in else if with comparator mark and another in the next line with assingment mark.
Since in menu.style.width == "-250px" you would be assigning value to -250 (menu.style.width == "0px) would never pass and it would keep assigning value -250 whenever someone clicks the button, but that fails, because you have a typo in your assingment mark.
Width cannot be negative
Is let compatable with your browser?
Have you considered using display:none and display:block (or whatever display you have)?
function toggleMenu() {
var menu = document.getElementById('mySidenav');
if (menu.style.display == "none")
menu.style.display = "inline-block"; //block
else
menu.style.display = "none";
}
window.onload = function() {
document.getElementById("MyElement").onclick = toggleMenu;
}
your else statement section seems to have the comparator (==) and assignment (=) the wrong way around :
} else if (menu.style.width = "250px") {
menu.style.width == "-250px";
}
looks like it should be
} else if (menu.style.width == "250px") {
//setting a negative width will immediately break the toggle logic.
//just set it to 0px.
menu.style.width = "0px";
}
Full example :
function changeClass() {
var sidenavElement = document.getElementById("mySidenav");
if(sidenavElement.style.width == "0px")
{
sidenavElement.style.width = "250px";
}
else
{
sidenavElement.style.width = "0px";
}
}
window.onload = function() {
document.getElementById("MyElement").addEventListener('click',changeClass);
}
#mySidenav
{
overflow:hidden;
height:100px;
background:green;
}
<div id="mySidenav" style="width:0px">
HELLO I AM SIDENAV
</div>
<button id="MyElement">toggle it</button>
There is many problem in your code, you have 2 elements inside your DOM with same ID as mentioned by Marek Maszay.
Your else statement should use == in condition and = for assignation.
Last thing, you should not use width property to display or not an element there is a display property in css
<html>
<body>
<div id="mySidenav" class="sidenav">
<span class="closebtn" id="MyElement">×</span>
<span id="menu">
About
Services
Clients
Contact
</span>
</div>
<!-- Use any element to open the sidenav -->
<span id="MyElement2">×</span>
<script type="text/javascript">
function toggleMenu() {
let menu = document.getElementById('menu');
if (menu.style.display == "") {
menu.style.display = "none";
} else if (menu.style.display == "none") {
menu.style.display = "";
}
}
window.onload = function() {
document.getElementById("MyElement").addEventListener('click',toggleMenu);
}
</script>
...
</body>
</html>
When display value is "", it take the default display value of an element, when its value is none the element is not displayed.
Here is your code modified.
EDIT
For some reason changing width to 0px doesn't hide the element. Element change from line to column, it seem that the browser try to fill element with content and put at least one word per line.
Seem like other people on web are having same problem.

Display content that was initially hidden onload

resultTable is initially hidden. When play button is clicked, the game will run and it will display the finalscore inside the hidden content. I need a function to display the finalscore. Currently the showResult function I have is not working (obviously)
I deleted unnecessary contents because the whole thing is a little big and messy. Hope the code still makes sense.
<body onload="loadGame()">
<div>
<button onclick="playButton()" id="play">Play</button>
</div>
<div id="resultTable" >
<span id="result"></p>
</div>
<script>
function loadGame(){
var hideResult = document.getElementById("resultTable");
hideResult.style.display = "none";
}
function playButton(){
playGame();
showResult();
}
function playGame(){
/*Some code here*/
document.getElementbyId("result").innerHTML = "finalscore";
}
function showResult(){
var show = document.getElementById("resultTable"); //fixed.
show.style.display = "block";
}
</script>
</body>
Change the way you are finding the element to set its display as block or none. You are using the "getElementsByClassName" method, but there is no element with such classname in the DOM.
Moreover, the "getElementsByClassName" will return you an array of all the elements, if found, in the DOM and you have to loop through the array to access it.
function showResult(){
document.getElementById("resultTable").style.display = "block";
}

How to toggle <p> once you click on <button> for second time?

function testFunction(){
var x = document.getElementById("test");
x.style.fontSize = "40px";
x.style.color = "blue";
}
<p id= "test"> **I would like to hide this once user clicks on button for a second time!!** </p>
<button type= "button" onclick="testFunction()"> Click Here! </button>
I am wondering how I hide the paragraph once a user clicks on the button a second time? When user clicks on button the first time, javascript is enabled and the code is shown, but then paragraph stays on the screen even after user clicks for a second time on button. Is there some way I can hide what is displayed if user clicks on button for a second time?
<p id= "test"> **I would like to hide this once user clicks on button for a second time!!** </p>
<script>
function testFunction(){
var x = document.getElementById("test");
x.style.fontSize = "40px";
x.style.color = "blue";
}
</script>
<button type= "button" onclick="testFunction()"> Click Here! </button>
First of all, I don't think that you should alter the style of elements trough js, that is what css is for (exceptions exist obviously). You could however alter the state of an element, and have your css react to that. I find it keeps your code a lot easier to maintain, and you know automatically where to look for what when you need to change something.
Have a look at the fiddle I prepared: http://jsfiddle.net/7xy39ufz/1/
So I added a state to your markup (I went for a data attribute, but a class or something could do as well)
<p id="test" data-visible="0">...</p>
<button type="button" id="button">...</button>
Then in the css I added a few lines that would react to the state:
p {
font-size: 50px;
color: blue;
}
p[data-visible="0"] {
display: none;
}
p[data-visible="1"] {
display: block;
}
And with all that done the javascript becomes very simple
document
.getElementById('button')
.addEventListener('click', testFunction, false);
function testFunction(){
var x = document.getElementById("test");
x.dataset.visible = x.dataset.visible == 0 ? 1 : 0;
}
Note that I moved the binding of the click event to js as well, in part because I couldn't get it to work in the fiddle (a scope / sandbox issue i guess), but mainly because I find js belongs with js, not in your markup.
Update
The real 'magic' is indeed being done in this line:
x.dataset.visible = x.dataset.visible == 0 ? 1 : 0;
This is basically a short hand for
if (x.dataset.visible == 0) {
x.dataset.visible = 1;
} else {
x.dataset.visible = 0;
}
(look up 'ternary' if you want to learn more about the syntax)
This code switches the data-visible attribute of you p between 1 and 0. The css reacts to that by setting the display property of that paragraph (that is what the attribute selector [data-visible="..."] is for).
I hope this clarifies things for you. Feel free to ask if you want me to explain further.
<script>
function testFunction(){
if (x.style.fontSize == "40px"){ //you could use another condition, or a global var here
document.getElementById("test").style.display="none";
}
else{
var x = document.getElementById("test");
x.style.fontSize = "40px";
x.style.color = "blue";
}
}
</script>

Categories