If you need data for multiple fields, then a 3rd option is to create a
single hash/data object for the page and writing all your data into
that. This makes your data easy to read and debug, and is highly
efficient because you don't have to 'parse' anything...

var Record = {
    foo:  "db-value-1"
,   bar:  db-value-2
,   baz:  db-value-3
}

Then you can use *either* jQuery or inline events to access this
data...

$(document).ready(function() {
     $("#myID").click(function() {
          alert( "foo = " + Record.foo );
          return false;
     });
});

You could use the fieldnames as keys in your data object to make it
generic...

var Record = {
    myID1:  "db-value-1"
,   myID2:  "db-value-2"
,   myID3:  "db-value-3"
    ...
}

$(document).ready(function() {
     $("a.linkType").click(function() {
          alert( this.id + " = " + Record[ this.id ] );
          return false;
     });
});

/Kevin


On Sep 23, 5:57 pm, Ricardo Tomasi <ricardob...@gmail.com> wrote:
> Do you really need to output this data embedded in the HTML? Does it
> have any meaning/purpose without Javascript? There are two simple non-
> hackish ways you can do it:
>
> 1:
> load data later via XHR, use an element identifier to bind it
>
> 2:
> output metadata in the class attribute - it's valid, and not against
> the specification - the class attribute is not specifically meant for
> presentation, the specs say "For general purpose processing by user
> agents".
> ex: class="link { foo: 'bar', amp: 'bamp', x: [1,2,3] }".
>
> It's easy to create your own parser for that:
>
> $.fn.mdata = function(){
>    return window["eval"]("(" + this[0].className.match(/{.*}/) + ")");
>
> };
>
> $('a').mdata() == Object foo:'bar' etc..
>
> It will work as long as you use valid JSON.
>
> cheers,
> Ricardo
>

Reply via email to