What is the best way to use JavaScript to conceal a section of a form div?

After receiving some code from a certain platform and implementing it to hide part of my form div element, I noticed that the element hides and unhides quickly when running in a browser. This rapid hiding and showing behavior occurs when clicking on the button to hide it. How can I solve this issue?

The details about what steps I have taken so far are outlined below:

.hide{

  display: none !important;
  visibility: hidden !important; 
}

.show{

display: block;
visibility:visible;

}

<!-- begin snippet: js hide: false console: true babel: false -->
<html>
<head>
    <title>

    </title>

    <script src="jquery/jquery-3.4.1.min.js"></script>
    <script src="jquery/jquery-3.4.1.slim.min.js"></script>
     <link rel="stylesheet" href="test.css">

    <script>


        $(document).ready(function() {
          //console.log('hello');
       
          $("#btn").on("click", function(){
            $("#form1").addClass("hide");
          })

        });

        
         //document.getElementById("form1").style.visibility="hidden";
          //document.getElementById("form1").style.display="none";
          
      
        </script>
        


</head>
<body>
<form>

<div id= "form1" class="show">
<label>Name</label>

<input type="text"/>
</div>

<br> <br>

<div id="form2">
<label>Email</label>

<input type="text"/>

</div>


<button type="submit" id="btn">more</button>


</form>


</body>


</html>

Answer №1

Ensure the button type is set to type="button"

<button type="button" id="btn">more</button>

Answer №2

There are several ways to improve this code, but if you simply want it to function properly, consider moving your button outside of the form or update your event listener as shown below:

$("#btn").on("click", function(e){
  $("#form1").addClass("hide");
  e.preventDefault();
})

The issue is caused by the button submitting the form and resulting in a page refresh.

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 loading data.json file in React.js and jQuery intergration

Having a background in mobile development, I am relatively new to web development so please excuse any amateur questions. Currently, I am working on a react.js project using create-react-app (which utilizes Babel). I am following a tutorial that requires ...

How jQuery manipulates the DOM during a drag-and-drop operation

My current challenge involves implementing jquery-ui sortable on items that appear while scrolling. Below is the code snippet I am using: var gridTop = 0, gridBottom = container.outerHeight(); $('#play-list').on('scroll', ...

Unable to append item to JavaScript Map

Currently, I am attempting to insert an item into a Map (similar to a dictionary) using JavaScript within a React Native environment. import React, { Component } from 'react'; import { AppRegistry, View, Button, } from 'react-native& ...

What is the process of transforming a string into an angular binding?

I have a variable called message that contains the text "you are moving to {{output.number}}". I attempted to insert this into a div element using $("#message").html(message); However, it just displayed the entire string without replacing {{output.number ...

Run a JavaScript code to show a hidden element

My latest project involves automating tasks on a website using Selenium. I encountered an issue where I cannot click on a hidden button defined this way: <body> <div class="smenu" id="smenu4"> <input tabIndex="-1" type="button" ...

Troubleshooting: ReactJS CSS Class Issue

I'm fairly new to working with ReactJS and I've encountered an issue while trying to create a class for a specific form. Below is the code I've written: import React, { Component, PropTypes } from 'react'; import s from './s ...

What is the best way to upload a file to a server while working with Vue.js in the Filemanager plugin, or how can I access a global function

How can I upload a file to the server using Vue.js in the Filemanager plugin? I have tried multiple methods and the console log shows that the upload was successful, but I am not able to see any files on the server. Does anyone know what could be wrong? & ...

The Dojo claro css method utilizes absolute positioning for styling ContentPane elements

I am currently utilizing dojo 1.8 and facing an issue with unwanted padding in my bordercontainer/contentpane layout. The problem arises when I incorporate the claro css file, as it seems to apply styles directly inline to the div elements used for my cont ...

What is the process for customizing styles within the administrative area of a Wordpress website, such as modifying the shade of the admin toolbar?

Can someone provide guidance on changing the color of a specific dashicon in the WordPress admin toolbar? I've tried adjusting the color using the browser inspector, but I can't seem to get it to work when I add the code to my stylesheet. #admin ...

Encountering an undefined property error while using Array.filter in Angular 2

hello everyone, I am currently faced with an issue while working on a project that involves filtering JSON data. When using the developer tools in Chrome, it keeps showing me an error related to undefined property. chart: JsonChart[] = []; charts: JsonC ...

Breaking the line when combining an image_tag and text in a Rails navbar

Looking for some help combining a logo and brand name in my Bootstrap navbar. I'm running into an issue where there is a line break separating the logo and text. Can someone provide some assistance? <%= link_to image_tag("brand_logo.png") + " Bran ...

Is it possible to use a server action in Next.js to both retrieve data and refresh the page?

I am currently working on developing a web application using Next.js (13.4.9) and I intend to utilize server actions. These server actions will involve sending data to the server, which will process the data and return a string back to me. The challenge I& ...

Tips for transforming an Observable stream into an Observable Array

My goal is to fetch a list of dogs from a database and return it as an Observable<Dog[]>. However, whenever I attempt to convert the incoming stream to an array by using toArray() or any other method, no data is returned when calling the retrieveDo ...

Transformation of looks post a refresh

Initially, the CSS and appearance of the page look fine when I first open it (after clearing the cache). However, upon refreshing the page, a part of it changes (specifically, the padding direction of a div). This change occurs consistently with each refre ...

Hiding a Blogger section when its widget is not visible

Take a look at this code: <b:section id='page-list'> <b:widget id='PageList1' locked='false' type='PageList' version='2' visible='true'> </b:widget> </b:section> I wa ...

Utilize a while loop in JavaScript to trigger a message when a variable dips below zero

Forgive me if I seem clueless. I am a beginner in the world of Javascript and am currently experimenting with loops. At the moment, I am toying around with this particular piece of code: <!DOCTYPE html> <html> <body> <button oncl ...

Encountering a 404 Error with jQuery Ajax Call Version 3.5.1

The ajaxCall function is returning a 404 error during the onChange select event. My application uses Spring Boot + BootStrap + Thymleaf + jQuery 3.5.1. The URL parameter corresponds to an internal spring boot @RequesteMapping. ERROR: jquery-3.5.1.min.j ...

Error in viewpoint/transformation transition on Microsoft Edge

Encountering a peculiar issue with MS Edge involving the animation of a door (a div tag) around the Y axis using the css rotateY() function in combination with perspective properties. The problem arises when transitioning an angle from a positive value to ...

JavaScript tip: Improve the way you highlight the current navigation page while scrolling by finding alternative methods to using "scrollY > x"

Currently, my webpage layout is divided into 4 sections - HOME, ABOUT, SKILLS, and CONTACT. Below is the JavaScript code I am using to highlight a specific section on the navigation bar based on the scroll position: let home = document.querySelector(" ...

Reposition and resize an image. Creating a grid layout

I'm having trouble with positioning and cropping an image correctly on my website. I want it to look like this (hero-section): The entire project is based on a grid layout. I need a circular image that is larger than the container, positioned to the ...