1

I have a DIV which has a display set to none, by using javascript I tried showing it by using the onclick of a button. But what happens is the exact opposite. My DIV is already shown and when I click the button it hides the DIV. What am i doing wrong here, please HELP!

This is my button and the div:

<button onclick="myFunction()">SHOW</button>
    <div id="how_to_form">
      <img src="../images/view.png">
    </div>

This is the JS code:

function myFunction() {
     var x = document.getElementById("how_to_form").style.display = 'none';
     if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
    }
  }
3
  • 2
    You are assigning x to the result of = 'none'. At that point, x is the string 'none' and has no style property Commented Apr 1, 2019 at 1:08
  • Yes I assigned it using .style.display. Is that the wrong way of assigning its display settings? Commented Apr 1, 2019 at 1:12
  • Change variable x value to var x= document.getElementById("how_to_form") Commented Apr 1, 2019 at 1:12

3 Answers 3

2

Your line there has a double assignment:

var x = document.getElementById("how_to_form").style.display = 'none';

It first assigns the display to none:

document.getElementById("how_to_form").style.display = 'none';

and then takes the result of that expression (which is the string you assigned), and assigns it to x:

var x = 'none';

Which isn't what you want. First declare the variable for the element, then assign its style.

Also, it sounds like you want the element to start out hidden - assign its initial style outside the function:

const form = document.getElementById("how_to_form");
form.style.display = 'none';
function myFunction() {
  if (form.style.display === "none") {
    form.style.display = "block";
  } else {
    form.style.display = "none";
  }
}

Or, to be more concise, use the conditional operator:

function myFunction() {
  form.style.display = form.style.display === "none"
  ? 'block'
  : 'none';
}

Also consider attaching the handler properly using Javascript, rather than using inline HTML attributes, which are generally considered to be pretty poor practice and can be hard to manage:

document.querySelector('button').addEventListener('click', myFunction);
Sign up to request clarification or add additional context in comments.

8 Comments

About this handler property, Is that an alternate way in creating a button?
That's the alternate (better) way to attach the handler, rather than using an inline HTML attribute.
But wouldn't it be harder to style the button? If I am styling it using CSS?
Not at all - all you have to do is remove the onclick="myFunction()" and use the line document.querySelector('button').addEventListener('click', myFunction); instead, you don't need to make any other changes.
Did you mean like this? <button type="submit" document.querySelector('button').addEventListener('click', myFunction());
|
1

have you checked if you have already set a default style to your div? you either have to set your div's default style to display:none by inline

<div id="how_to_form" style="display:none">

or by css

<style>
    #how_to_form{ display:none }
</style>

1 Comment

Oh thanks @pokken! This works! Did not thought css would do the thing. But can I ask what is wrong with my display = 'none' in my javascript code?
1

Issue is your variable assignment

Chaining the assignment operator is possible in order to assign a single value to multiple variables.

Please refer this link for variable assignment options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators

var x = y= 10

Then both x and y values are 10

Similarly in your code var x is none

To achieve expected result , use below option
1. set CSS for html_to_form to display:none 2.In your code change variable x assignment to var x = document.getElementById("html_to_form")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.