Dynamically hide SharePoint input fields on NewForm or EditForm

Suppose you have a NewForm in a SharePoint list that contains a checkbox and several other input fields.

Whenever the checkbox is checked, some fields should be hidden. When unchecked, they shall be visible.

Also, if you have a lot of fields and the form opens as a modal dialogue, this dialogue should automatically be resized.

Here is one approach with JavaScript you can use. Copy the content to a txt file within the SiteAsstets library on the SharePoint site. Then, on the NewForm or EditForm, add a new CEWP (Content Editor Web Part) and reference to that txt file.

Note: I provided that code sample with the file extension .js for formatting purposes only. Please save the code with the .txt file extension.