jQuery in Action
56
CHAPTER 3
Bringing pages to life with jQuery
previous format of
attr()
; the function is invoked for each individual element in
the matched set.
WARNING
Internet Explorer won't allow the
name
attribute of
<input>
elements to
be changed. If you want to change the name of
<input>
elements in
Internet Explorer, you must replace the element with a new element pos-
sessing the desired name.
sessing the desired name.
Now we know how to get and set attributes. But what about getting rid of them?
3.1.4 Removing attributes
In order to remove an attribute from
DOM
elements, jQuery provides the
removeAttr()
command. Its syntax is as follows:
Note that removing an attribute doesn't remove any corresponding property
from the JavaScript
from the JavaScript
DOM
element, though it may cause its value to change. For
example, removing a
readonly
attribute from an element would cause the value
of the element's
readOnly
property to flip from
true
to
false
, but the property
itself isn't removed from the element.
Now let's look at some examples of how we might use this knowledge on
our pages.
3.1.5 Fun with attributes
Let's say that we want to make all links on our site that pointed to external
domains open in a new window. This is fairly trivial if we're in total control of the
entire markup, as shown:
domains open in a new window. This is fairly trivial if we're in total control of the
entire markup, as shown:
<a href="http://external.com" target="_blank">Some External Site</a>
Command syntax: removeAttr
removeAttr(name)
Removes the specified attribute from every matched element
Parameters
name
(String) The name of the attribute to be removed
Returns
The wrapped set
The wrapped set