Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a bug with .removeProp() in jQuery, or am I doing something wrong?

Tags:

jquery

Selected items in the listbox will show up in the UL below. Click on an item in the UL and it will remove it from the listbox and remove itself from the UL.

Reproduce Bug: http://jsfiddle.net/rkw79/mmBKf/2/

  • Select an item in the listbox
  • Click on that item in the UL, it will disappear and the listbox will show it as unselected
  • Click on that same item in the listbox

Notice that the event is fired, but the item is not added

Now do the same steps, except use .prop('selected','') instead of .removeProp('selected'): http://jsfiddle.net/rkw79/mmBKf/3/

like image 767
rkw Avatar asked Dec 09 '11 03:12

rkw


People also ask

How to remove Prop using jQuery?

jQuery removeProp() MethodThe removeProp() method removes a property set by the prop() method. Note: Do not use this method to remove HTML attributes like style, id, or checked. Use the removeAttr() method instead.

How to get value of property in jQuery?

jQuery prop() Method The prop() method sets or returns properties and values of the selected elements. When this method is used to return the property value, it returns the value of the FIRST matched element.

What is the difference between ATTR and prop in jQuery?

prop() method provides a way to explicitly retrieve property values, while . attr() retrieves attributes. For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the . prop() method.


2 Answers

Everything you need to know is in the documentation.

With some built-in properties of a DOM element or window object, browsers may generate an error if an attempt is made to remove the property. jQuery first assigns the value undefined to the property and ignores any error the browser generates. In general, it is only necessary to remove custom properties that have been set on an object, and not built-in (native) properties.

Note: Do not use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.

like image 73
RightSaidFred Avatar answered Sep 28 '22 00:09

RightSaidFred


You should be using removeAttr()

$('myele").attr('selected'); // <myele /> --> <myele selected />

$('myele").removeAttr('selected'); // <myele selected /> --> <myele />

like image 43
Terry Avatar answered Sep 28 '22 01:09

Terry