[jQuery] $('').attr('', '') - why no direct method to get/set html attributes?

[jQuery] $('').attr('', '') - why no direct method to get/set html attributes?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2800.1543" name=GENERATOR></HEAD>
<BODY>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>I am confused and
feel like I must have missed something - I've read through the jquery docs on
the site and can't find the functionality I'm after.</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>Basically we
have:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>$('').html('') -
this gets/sets innerhtml content</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>and we
have:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>$('').val('') - this
gets/set the value of form input fields</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>but why is there no
methods for setting and retrieving html attributes and
properties?</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>I propose that there
should be:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006>$('selector').attr('name','value') - to get/set html
element attributes (optional second argument)</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006>and
also:</SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN
class=208181519-03082006></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>$('selector').prop('name','value') - to
get/set dom object properties (optional second
argument)</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>Just in case nobody is aware you can set properties on
dom objects, imagine this:</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>document.getElementById('bob').colors =
['red','green','blue'];</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>this attaches an array to the property 'colors' on the
html element 'bob' - this can be quite handy
sometimes.</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>if you want to set html element attributes the best
method is to use setAttribute - this only accepts strings as far as I'm aware,
but sometimes it is convenient to attach any type of object to an element
(array, object, function) whatever.</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>you can even do things like:</SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>var bobElm =
document.getElementById('bob');</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT><FONT face=Arial
size=2><SPAN class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006>bobElm.whoami =
function(){ alert(this.id) };</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>bobElem.whoami();</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>I don't know if this is considered 'good practice', but
sometimes it is so immensely useful to store stuff on the dom objects they
relate to instead of linking stuff in variables and
arrays.</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>I guess the functionality isn't there because
ultimately when retrieving properties you can only easily handle one
object at a time and hence maybe the following is
easier:</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>$('').get(0).getAttribute('');</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>And I know there are methods in jquery for
setting html attributes already.</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>Plus I guess if you want to check loads of elements
attributes you could just do:</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>$('').each(function()</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>{</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>  
alert(this.getAttribute(''));</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>});</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>So maybe it wasn't an
oversight.</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>What do other people think about
this?</SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006></SPAN></SPAN></SPAN></SPAN></FONT> </DIV>
<DIV><FONT face=Arial size=2><SPAN class=208181519-03082006><SPAN
class=208181519-03082006><SPAN class=208181519-03082006><SPAN
class=208181519-03082006>Jon</SPAN></SPAN></SPAN></SPAN></FONT></DIV></BODY></HTML>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/