• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JQuery remove(), removeAttr() and removeClass() Example

May 7, 2014 //  by Krishna Srinivasan//  Leave a Comment

JQuery Remove() Method

The remove() method is same as detach() method which removes the selected elements including all text and child nodes from the DOM. But it won’t keep all data and events associated with removed elements in the set of matched elements from the DOM completely. It restores elements data, not its event handlers. All events and data associated with elements are removed.

JQuery Remove() Syntax

$(selector).remove();

It does not contain any parameters.

JQuery Remove() Example

<!doctype html>
<head>
<title>JQuery Remove Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Remove Method Example</h2>
<script type="text/javascript">`
   $(document).ready(function(){
     $("button").click(function(){
       $("h3,h4").remove();
      });
   });
</script>
<body>
   <button>Click to remove the elements</button>
   <h3>Hello world!!!</h3>
   <h4>Welcome to JQuery!!!</h4>
</body>
</html>
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“button”).click(function()) line defines click event which occurs when button is clicked.
  • $(“h3,h4”).remove(); line defines remove() method which removes the selected elements from the DOM.
  • When we run the above script, a button will appear on the browser. If we click on the button, all elements will get removed from the DOM.

When you run the above example, you would get the following output:

  • Run This Demo

JQuery Remove Method Example

JQuery removeAttr() Method

JQuery removeattr() method is used to remove attribute from the selected elements in the set of matched elements. It uses removeAttribute() function which can be called directly on jQuery object. It’s going to remove the attribute which is specified in the parameter of removeAttr() method.

JQuery removeAttr() Syntax

$(selector).removeAttr(attribute);

It contain parameter called attribute is required parameter which specifies one or more attributes to remove.

JQuery removeAttr() Example

<!doctype html>
<head>
<title>JQuery Remove Attribute</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Remove Attribute Example</h2>
<script type="text/javascript">
   $(document).ready(function(){
      $("#myattribute").click(function(){
        $("h4").removeAttr("style");
      });
   });
</script>
<body>
       <button id="myattribute">Remove the attribute</button>
        <h4 style="background:orange;">Welcome to JQuery!!!!</h4>
</body>
</html>

JQuery RemoveAttr() Conclusion

  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“#myattribute”).click(function()) statement defines click event which occurs when button is clicked.
  • $(“h4”).removeAttr(“style”); line defines removeAttr() method to remove attribute from the selected elements. Here, it will remove style attribute which is specified in the h4 tag.
  • After executing the above script, a button will get display on the browser. When you click on the button, it will remove the attribute from the selected elements. Before clicking on the button, background color of the line will be orange. When you click on the button, background color will get removed.

When you run the above example, you would get the following output:

  • Run This Demo

JQuery Remove Attribute Method Example

JQuery removeClass() Method

JQuery has provided method called removeClass() to remove CSS class from the selected elements in the set of matched elements. The class name which is defined in the parameter will be going to remove from the selected elements. Suppose if no class names are defined, then it will remove the all classes from the DOM.

JQuery removeClass() Syntax

$(selector).removeClass(classname);

It has parameter called classname is required parameter which removes one or more css class from the each of the matched elements.

JQuery removeClass() Example

<!doctype html>
<head>
<title>JQuery Remove Class</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Remove Class Example</h2>
<style type="text/css">
   .highlight
   {
      background:green;
   }
</style>
<script type="text/javascript">
   $(document).ready(function(){
      $("#myremoveclass").click(function(){
        $("h4").removeClass("highlight");
      });
   });
</script>
<body>
       <button id="myremoveclass">Remove Class</button>
        <h4 class="highlight">Welcome to JQuery!!!!</h4>
</body>
</html>
  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“#myremoveclass”).click(function()) statement defines click event which occurs when button is clicked.
  • $(“h4”).removeClass(“highlight”); line defines removeClass() method which removes CSS class from the selected elements. Here, it’s going to remove class called highlight from the DOM.
  • The highlight class contains background color green for the line Welcome to JQuery!!!! .When you run the above script, a button will appear on the browser. When you click on the button, it will remove the class from the selected elements.

When you run the above example, you would get the following output:

  • Run This Demo

JQuery Remove Class Method Example

Category: jQueryTag: JQuery Selectors

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: «jquery How To Select Multiple Elements using JQuery Selector
Next Post: Spring Roo + Spring Data + MongoDB Repositories Integration »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact