Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9003595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:43:56+00:00 2026-06-16T00:43:56+00:00

Why jQuery do not remove attribute name for class or style when there is

  • 0

Why jQuery do not remove attribute name for class or style when there is no attribute value?

For example, $('<div>').addClass('foo').removeClass('foo') returns [<div class>​</div>​] and not [<div>​</div>​]

My guess is performance, but maybe there are other reasons.

Here is jQuery implementation:

https://github.com/jquery/jquery/blob/master/src/attributes.js#L88

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:43:56+00:00Added an answer on June 16, 2026 at 12:43 am

    Compare (from Chrome 23):

    > $('<div>').addClass('foo').removeClass('foo').attr('class')
    ""
    

    and:

    > $('<div>').attr('class')
    undefined
    

    In the former case, the element still has a class attribute, but it has no value.

    In the latter case, element has no class attribute at all.

    What’s happening is that jQuery uses simple string manipulations to remove classes, and if the resulting string is empty then that’s what’s used – it never removes the class attribute entirely.

    In Chrome, even the DOM3 classList functions leave behind an empty class attribute – calling el.classList.add('foo') followed by el.classList.remove('foo') will (in the absence of any other classes) leave you with an empty class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Popups in jQuery Mobile not working as intended, please advice. The popup DIV is
I use jquery validation plugin and the remote attribute works with emailId but not
<div class=views style=position: relative; width: 421px; height: 15px; overflow: hidden;> TEST TEXT </div> How
How can you remove the attribute id by jQuery? jQuery('a.no_flag_question').live('click', function(){ jQuery.post('/codes/handlers/no_flag_question.php', { question_id:
Please consider following snippet: <div class=row> <div class=span12> <div class=control-group> <label class=control-label>Person Name</label> <div
I know there's the possibility, using jQuery, to mix Multiple Selector with Multiple Attribute
DIV Tag is not displaying according to the screen size. I am using jquery
I am trying to remove an attribute of all elements that match the class
Trying to wrap my head around the jQuery .not() function, and running into a
this is the problem: I'm using javascript and JQuery (not UI) to drag nested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.