Unable to set DIV to 'inline-block' or none based on checkbox selection

Despite reviewing multiple examples, I am still struggling to make this DIV visible and hidden by clicking a checkbox. Can someone please review my JavaScript code?

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SimpleSample - Hidden Div</title>
</head>

<script type="text/Javascript">
function showhideDiv1() {
   if (document.getElementyById('div1').style.display = "none") {
      //show the div:
      document.getElementById('div1').style.display = "block";
   }
   else {
      //hide the div:
      document.getElementById('div1').style.display = "none";
      //clear the form:
      //document.getElementById('myform').reset();
   }
}

function showhideDiv2() {
   if (document.getElementyById('div2').style.display = "none") {
      //show the div:
      document.getElementById('div2').style.display = "block";
   }
   else {
      //hide the div:
      document.getElementById('div2').style.display = "none";
      //clear the form:
      //document.getElementById('myform').reset();
   }
}
</script>

<body>
<form id="myform" name="myform" method="post" action="">
  <div id="div1">
    <p>Content for  id "div1" Goes Here</p>
    <table width="800" border="0">
      <tr>
        <td>First Name</td>
        <td><input name="fname" type="text" id="fname" value="" /></td>
      </tr>
      <tr>
        <td>Last Name</td>
        <td><input name="lname" type="text" id="lname" value="" /></td>
      </tr>
    </table>
   </div>

    <div id="div2">
      <p>Content for  id "div2" Goes Here</p>
      <table width="800" border="0">
        <tr>
          <td width="258">Email</td>
          <td width="532"><input name="emailAddr" type="text" id="emailAddr" value="" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
      </table>
    </div>

    <div id="div3">
      <p>Content for  id "floatingDiv" (when ready) Goes Here</p>
      <table width="800" border="0">
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><input onchange="showhideDiv1();" name="showDiv1Chk" type="checkbox" id="showDiv1Chk" checked="checked" />
            - Show Div1</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><input onchange="showhideDiv2();" name="showDiv2Chk" type="checkbox" id="showDiv2Chk" checked="checked" />
            - Show Div2</td>
          <td> &nbsp;</td>
        </tr>
      </table>
    </div>
    <p>&nbsp;</p>

</form>
</body>
</html>

Your assistance is greatly appreciated!

Answer №1

There are two instances where you mistakenly wrote "getElementyById" instead of "getElementById."

In addition, when writing if statements, remember to use "==" for comparison, not "=".

Incorrect:

if (document.getElementById('div1').style.display = "none")

Correct:

if (document.getElementById('div1').style.display == "none")

After making these corrections, the code should work correctly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble establishing a new local Windows directory structure with Selenium

After following the guidelines provided here and here, I am striving to ensure that the directory where my results reports are stored is consistently available for each user. new File(sampleFolder).mkdir(); The sampleFolder path displayed in the Eclipse ...

Previewing an uploaded image before submitting with FileBase64: A step-by-step guide

How can I implement a preview for an uploaded image before submitting the form? I have the upload functionality working but I would like to add a preview feature for the image. Below is the code snippet I am currently using: const [shop, setShop] = us ...

What is the best way to navigate users to a different page when they click on a button?

I recently designed a button in Dreamweaver software. Could you please guide me on how to set up the button so that when clicked, it redirects the user to a different page using Dreamweaver? Below is the HTML code for the button: <input type="submit" ...

Differences between async/await and then methods in React, demonstration shows that only async/await is functional. Why is

Trying to understand and implement two different API data fetching methods in React app development. The first method involves using the JavaScript Fetch API, while the second method utilizes the async/await syntax. I am currently experimenting with both a ...

Issue with retrieving the positions of two numbers in an array

I encountered a challenge: I have an array of integers nums and an integer target. My goal is to find the indices of two numbers in the array that add up to the specified target. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Output: Thi ...

struggling to adjust image dimensions using bootstrap styling

Currently, I am in the process of creating a Carousel image slider using bootstrap. However, I am facing an issue with changing the height of the images within the carousel. Whenever I try to adjust the height, it ends up affecting both the width and heigh ...

What methods can I use to retrieve traversed objects using riak-js?

Utilizing Node with riak-js to interact with a Riak database. I have established two buckets named invites and events. The invites bucket contains a link to events. Is there a way to fetch both the invite object and the corresponding event object in one qu ...

Narrow down an array of objects by matching a specific property and value to a separate array of objects

I am facing a challenge with two sets of arrays - one for the headers (columns) of a table and the other for the rows. const headers = [ { text: 'Dessert (100g serving)', align: 'left', sortable: false, value: 'n ...

Guide to dynamically add data to two columns

I have a challenge with organizing multiple inputs into rows with two columns each, ensuring that each input is appended to the appropriate side (50% for each column unless there's an odd number of inputs). Currently, the inputs are being added to se ...

"Enhance Your Website with jQuery: Organized Lists with Fixed Length, Connected,

I currently have a list that is separated into three columns, structured as follows: Column1 Column2 Column3 1 4 7 2 5 8 3 6 9 The list ranges from 1 to 9 and each column consists of a fixed number of rows (3). I am lo ...

Watchman encountered an error upon initialization

Hi there, I'm encountering an error when trying to start my app with 'react-native start'. Can anyone provide guidance on how to resolve this issue? I attempted changing the permissions of the watchman folder and project folder using chmod - ...

Guide on implementing jQuery Validation plugin with server-side validation at the form level

Does anyone have a suggestion for how to handle server-side validation errors that occur after passing the initial client-side validation on a form? $("#contact_form").validate({ submitHandler: function(form) { $.ajax({ type: 'POST', ...

Issues arise while utilizing the execute method in PHPUnit-Selenium2

When creating Acceptance tests with PhpUnit and its Selenium2 extension, I am attempting to utilize the execute method within the PHPUnit_Extensions_Selenium2TestCase class to run Javascript code that checks if the document is fully loaded. Here is an exa ...

Tips on utilizing commas as text input within an HTML input array

Is there a way to pass a comma in an input field to the server using input arrays? The server currently receives comma-separated input values from an array of inputs, which causes it to consider any commas within the input as separators. For example: HTM ...

AngularJS secureHTML verify image click event

Just starting out with AngularJS and hoping for some assistance :) I have a div that has the details of an activity, formatted in HTML. To display this content correctly, I am using trustAsHTML: <div class="description-div"> <div ng-bind-htm ...

The wordpress jquery dependency is failing to respond

After converting an HTML ecommerce template into WooCommerce, I am experiencing issues with the functionality. The Nivo slider and some other product features are not working properly. It seems like they are having trouble finding WordPress jQuery, even th ...

The voracious nature of the `+` and `*` operators

There is a variable, const input = "B123213"; When using the following regex pattern, const reg = /\d+/; and executing String match function, console.log(input.match(reg)); The output returned is 123213, illustrating that the expression is gree ...

What is the best way to display JQuery mobile tap event information in real-time?

I am experiencing an issue in my code where the tap event is being triggered, but the array[i] value is printed as null. Whenever I click on any index, it always prints an empty string (" "). Why does it display null instead of the clicked value? I am see ...

Utilize dynamic global variables in React that are provided during runtime, making them unpredictable during the build process

I am currently developing an application for Overwolf, which you can find more information about at For this platform, applications are built using html/js and run in the Overwolf Browser. The browser provides access to the Overwolf API through a global v ...

The value of type 'X' cannot be assigned to type 'Y' or 'undefined'

In my code, there is a component that requires a prop with an enum value: export enum AType { some = "SOME", word = "WORD", } const MyComponent = (arg: AType) => {} When I try calling this component like so: <MyComponent ar ...