SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Wizard aaron.martone's Avatar
    Join Date
    Feb 2003
    Location
    Florida, USA
    Posts
    2,322
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Passing .attr() an array to capture multiple values

    Now, I know that with methods like .css() you can pass it a single string to return the CSS property value on the matched elements like this:
    Code:
    var myColor = $("p").css("color");
    And I know you can pass it an array of properties for it to create an object literal, like this:
    Code:
    var myProperties = $("p".css([ "color", "font-weight", "font-style" ]);
    And that you can set properties by either a single property, or sending an object literal, ie:
    Code:
    $("p").css("color", "red");
    $("p").css({ color: "red", fontWeight: "bold", fontStyle: "italic" });
    But correct me if I'm wrong, I thought you could do all of the above with the .attr() method as well? Yet if I do:
    Code:
    var myAttribs = $("p").attr([ "id", "class", "style" ]);
    Then jQuery throws an error. I'm using 1.10 right now. Am I just shrooming that I thought .attr() supported this?

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by aaron.martone View Post
    Then jQuery throws an error. I'm using 1.10 right now. Am I just shrooming that I thought .attr() supported this?
    Sorry, but the attr documentation page doesn't seem to show any support for multiple terms.

    Using the jQuery source viewer you can see that with jQuery.css it has additional code to deal with arrays, whereas with jQuery.attr method there is no array support.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •