I''m looking for a clean way to add attributes to HTML elements in order to pass information to JavaScript. I''d like to write valid XHTML and therefore in the past have eschewed adding non-standard attributes. Instead, I encoded my additional information in the class attribute, which is technically valid, but doesn''t feel very clean nevertheless. Is there a nice, easy, and validating way of achieving what I''m trying to do? Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/
Just off the top of my head, I''m gonna go on a limb here and say you can use other means to pass this data to the js environment without mucking up your markup. That is assuming this is not really semantically relevant data, but rather some meta-information or application state stuff you''re trying to marshal back and forth. Why not simply render javascript tags packaged with this data ("script" is a valid xhtml tag ya know)... On 7/23/06, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > > I''m looking for a clean way to add attributes to HTML elements in order > to pass information to JavaScript. I''d like to write valid XHTML and > therefore in the past have eschewed adding non-standard attributes. > Instead, I encoded my additional information in the class attribute, > which is technically valid, but doesn''t feel very clean nevertheless. > Is there a nice, easy, and validating way of achieving what I''m trying > to do? > > Michael > > -- > Michael Schuerig > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org > http://www.schuerig.de/michael/ > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
> On 7/23/06, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:[passing information in additional, custom attributes] On Sunday 23 July 2006 21:09, Ryan Gahl wrote:> Just off the top of my head, I''m gonna go on a limb here and say you > can use other means to pass this data to the js environment without > mucking up your markup. That is assuming this is not really > semantically relevant data, but rather some meta-information or > application state stuff you''re trying to marshal back and forth. > > Why not simply render javascript tags packaged with this data > ("script" is a valid xhtml tag ya know)...Yes, I know, but I''d like to avoid sprinkling script elements all over the page. In particular, when it is really just declarative data I want to attach to elements. An example is type/format/length information for use in client-side validation. Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/
I''ve been using custom attributes for a while for various things including widgets and form validation. On projects other than .Net I would put them in namespaces. .Net seems to complain about having namespaced attributes on elements that runat="server". I see this as a perfect way of adding functionality to your document. Using the class attribute in my mind is messing with the specs. It is called eXtensible HTML afterall. If you really cared about validating your code, maybe for a big project with lots of contributors, you can just extend the XHTML 1 DOCTYPE to include your custom attributes and use a true XML validator. Validation isn''t the end all be all. It is a great starting point for people to begin to get an understanding of XHTML. One of the reasons I''ve done this is to production faster and easier on the rest of the front-end team. For example on the Validator. All they have to do is put validate="true" on the form tag and then various attributes on other elements throughout the form. Like required, datatype, etc. No JavaScript is required at all. Brandon On 7/23/06, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote:> > > > On 7/23/06, Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> wrote: > [passing information in additional, custom attributes] > > On Sunday 23 July 2006 21:09, Ryan Gahl wrote: > > Just off the top of my head, I''m gonna go on a limb here and say you > > can use other means to pass this data to the js environment without > > mucking up your markup. That is assuming this is not really > > semantically relevant data, but rather some meta-information or > > application state stuff you''re trying to marshal back and forth. > > > > Why not simply render javascript tags packaged with this data > > ("script" is a valid xhtml tag ya know)... > > Yes, I know, but I''d like to avoid sprinkling script elements all over > the page. In particular, when it is really just declarative data I want > to attach to elements. An example is type/format/length information for > use in client-side validation. > > Michael > > -- > Michael Schuerig > mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org > http://www.schuerig.de/michael/ > _______________________________________________ > Rails-spinoffs mailing list > Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs >_______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
The way I''ve been handling this is to allow the user to pass custom attributes and then remove them from the DOM using removeAttribute once I''m finished with them. For example, a wizard component we''re developing: <div class="wizard" title="Add User Wizard" current="2" links="1"> <ol> <li url="example.html" title="User Information" /> <li url="details.html" title="User Details" /> <li url="verify.html" title="Verification" /> <li url="confirm.html" title="Confirmation" /> </ol> </div> Once the wizard is rendered by the component javascript, I remove the "current" and "links" attributes. I then validate the generated code. I''m not sure if this is the best way, but it seems to be working well so far. Jeff Rankin Human Factors 544-4333 Michael Schuerig <michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org> To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Sent by: cc: (bcc: William J. Rankin/UPC) rails-spinoffs-bounces-1W37MKcQCpIbvKGg9ot7NQ@public.gmane.org Subject: [Rails-spinoffs] Custom attributes? onrails.org 07/23/2006 07:01 AM Please respond to rails-spinoffs I''m looking for a clean way to add attributes to HTML elements in order to pass information to JavaScript. I''d like to write valid XHTML and therefore in the past have eschewed adding non-standard attributes. Instead, I encoded my additional information in the class attribute, which is technically valid, but doesn''t feel very clean nevertheless. Is there a nice, easy, and validating way of achieving what I''m trying to do? Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs