SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: html attributes

  1. #1
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    html attributes

    Hey guys,

    Just getting some feedback...

    I currently do (for example):

    <a class="link" href="..." action="..." id="..." .... etc etc

    Or other tags, and add attributes to them, so then in jquery:

    Code:
    $(".link").bind("click", function() { 
        action = $(this).attr("action");
        ....
    });
    So I add attributes to HTML tags. This isn't W3C compliant.

    How would you go about doing such a thing? Suppose I need to click a link and pull that ID, or an option depending on the link, and I want it to post maybe.

    So what are some of the ways you do that?

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using JavaScript to add custom attributes (aka "expando properties") to an element doesn't have to be W3C compliant. Only the original markup does in order for it to validate.

  3. #3
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I'm adding the them right to the elements of the original markup, in my templates. So it doesn't validate. But I'm trying to find another way which will keep it compliant.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Modern browsers allow the HTML to be validated at any point during its display including after modification via JavaScript. If the HTML is not valid at any point then it is not valid. Why add invalid attributes when you can add classes to achieve the same thing AND still be valid?

    The standards specify that JavaScript should be able to add and update HTML attributes. There is no requirement for browsers to support adding garbage into the page and so browsers are allowed to ignore attempts to add anything that isn't a real attribute. That your code works now in some browsers doesn't mean it will work in all browsers or that future browsers will continue to support that non-standard processing.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Addict nabeel's Avatar
    Join Date
    Nov 2002
    Location
    in westchester county, ny
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    Modern browsers allow the HTML to be validated at any point during its display including after modification via JavaScript. If the HTML is not valid at any point then it is not valid. Why add invalid attributes when you can add classes to achieve the same thing AND still be valid?

    The standards specify that JavaScript should be able to add and update HTML attributes. There is no requirement for browsers to support adding garbage into the page and so browsers are allowed to ignore attempts to add anything that isn't a real attribute. That your code works now in some browsers doesn't mean it will work in all browsers or that future browsers will continue to support that non-standard processing.
    Well that's my question - instead of adding attributes, what are other ways to accomplish it?

    Like this example:

    Code:
    echo '<form action="'.$url.'" method="'.$method.'" class="form" name="'.$name.'"
                        id="'.$id.'" divupdate="'.$update_div.'" callback="'.$callback.'" '.$attrib.'>';
    As you can see I've added several attributes, where the values will change.
    So my question is - what is another *valid* way?

    You said in classes, how would I check for something as like a div name which could change?


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
  •