What is the difference between removeProp and removeAttr in jQuery 1.6?

If you remove the Prop for something that you should have used removeAttr() will this happen silently? Will this work? Will it actually remove the entire attribute, or just the value inside it?

If checked added using removeProp() , is it possible to remove it using removeAttr() ?

Many questions!

+8
javascript jquery html
source share
3 answers

An element attribute is something like a class. While its property will be "className".

This is the reason for adding jQuery.prop and jQuery.propHooks in version 1.6 to make working with them easier.

So, if the property has the same name as the attribute, you can use removeProp or removeAttr.

I asked a similar question on the jQuery forum, got this answer:

Yes, attr is for html attributes as they are strictly defined. support for properties. So, for example, let's say you have a node element with the class "something" (the source element is not a jQuery object). elem.className is the property, but where the attribute is. Changing a class attribute also changes the property automatically and vice versa. Currently attr is confused and confusing because it tried to work both functions and because of this many errors. the introduction of jQuery.fn.prop will solve several blockers, separate code because it had to be separated from the beginning and give developers faster functions to do what they expect them to do. Let me make a percentage per second and say that from my experience in supporting IRC and reading other code, 95% of the use cases for attr do not have to switch to support.

EDIT

It is best to use jQuery.attr or jQuery.prop. It seems that when setting up and deleting the checked attribute, it seems something strange using both.

See an example here: http://jsfiddle.net/tomgrohl/uTCJF/

There is an error in list 1.6: http://bugs.jquery.com/ticket/9079

+5
source share

The jQuery official blog provides a very clear explanation:

In release 1.6, we split the processing of DOM and DOM attribute attributes into separate methods. the new .prop () method sets or gets properties on the DOM elements and .removeProp () removes the properties. In the past, jQuery did not draw a clear line between properties and attributes. Typically, the DOM attributes represent the state of the DOM information as extracted from the document, such as the value attribute in the markup, the DOM properties represent the dynamic state of the document; for example, if the user clicks in the input element above and the types are def .prop("value") abcdef, but .attr("value") remains a.

In most cases, the browser considers the attribute value as the starting value for the property, but Boolean attributes such as checked or disabled have unusual semantics.

For example, consider the markup <input type="checkbox" checked> . the presence of a verified attribute means that the DOM .checked property is true, although the attribute does not matter. In the code above, the checked attribute value is an empty string (or undefined if no attribute was specified), but the checked property value is true .

Before jQuery 1.6, .attr("checked") returns the value of the Boolean ( true ) property, but as of jQuery 1.6 it returns the actual value of the attribute (empty string), which does not change when the user clicks on change its state.

There are several alternatives for checking the current state of a flag. The best and most executor should use the DOM property directly, as in this.checked event handler, when it refers to the element that was clicked. In code that uses jQuery 1.6 or later, the new $(this).prop("checked") method gets the same value as this.checked and is relatively fast. Finally, the expression $(this).is(":checked") works for all versions of jQuery.

+7
source share

Using jQuery 1.6, I tried to clone a menu item that had several id attributes, and so I did this:

$('ul.menu').clone().filter('*').removeProp('id').appendTo('.sidebar');

When I inspected the Firebug elements, I had a lot of id="undefined" - not what I wanted. So now I am using removeAttr and it seems to work much better.

$('ul.menu').clone().filter('*').removeAttr('id').appendTo('.sidebar');

0
source share

All Articles