[jQuery] Re: Way to convert DOM event to jQuery event?
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
[jQuery] Re: Way to convert DOM event to jQuery event?
On Thu, Sep 24, 2009 at 12:10 PM, Kevin Dalman kevin.dal...@gmail.com wrote: 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... And while all of the above are true, a 4th (not necessarily better) option is to have a script block on the page that is NOT language=text/javascript. That results in a valid page that nonetheless has the data available to Jquery. http://ejohn.org/blog/javascript-micro-templating/ That said, however, I'd say: If the data is JS specific, drop it into the attribute data. If the data could be relevant to other consumers of the page (even if they don't exist yet) the suggestion for the class attr sounds solid. -- Brett Ritter / SwiftOne swift...@swiftone.org
[jQuery] Re: Way to convert DOM event to jQuery event?
On Tue, Sep 22, 2009 at 4:41 PM, WalterGR walte...@gmail.com wrote: On Sep 22, 4:35 pm, Ricardo ricardob...@gmail.com wrote: This doesn't make any sense. If you're already using jQuery why keep your handler in the HTML? Because I need to pass additional information to the event handler in a clean way. (i.e. rather than hacks using various attributes on the anchor.) Is there a non-hackish way to do this? Hi Walter: Sort of along the lines of what Scott had suggested, but rather than using a plugin, could you use jQuery's built in data() method? http://docs.jquery.com/Core/data#namevalue Assuming you're generating these links dynamically you could assign the 'foobarbaz' value to an element with a particular ID. $(document).ready(function() { $('#myID').data('myFirstArg', 'foobarbaz'); $('#myID').click(function() { alert($(this).data('myFirstArg')); return false; }); }); a href=# id=myIDshow details/a That's a fairly simplistic example. The example in the docs shows how you can store complex data using JSON. Would something like that work? Charlie -- Charlie Griefer http://charlie.griefer.com/ I have failed as much as I have succeeded. But I love my life. I love my wife. And I wish you my kind of success.
[jQuery] Re: Way to convert DOM event to jQuery event?
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 On Sep 22, 8:41 pm, WalterGR walte...@gmail.com wrote: On Sep 22, 4:35 pm, Ricardo ricardob...@gmail.com wrote: This doesn't make any sense. If you're already using jQuery why keep your handler in the HTML? Because I need to pass additional information to the event handler in a clean way. (i.e. rather than hacks using various attributes on the anchor.) Is there a non-hackish way to do this? Thanks for the code snippet - that's what I was looking for. Walter
[jQuery] Re: Way to convert DOM event to jQuery event?
or this a href=# rel=foobarbazshow details/a $(a).click(function(){ var rel = $(this).attr('rel'); } now you have passed foobarbaz to jquery. WalterGR wrote: I want to take advantage of jQuery's cross-browser Event object, but via an onclick event handler. For example: a href=# onclick=showTextNearThisLink(convertToJQueryEvent(event), 'foobarbaz');show details/a Is something like this possible? I may be thinking about the problem wrong. I understand that event handlers are usually wired up on $(document).ready, but I need to send additional information ('foobarbaz' above) to the event handler. Thanks, WalterGR
[jQuery] Re: Way to convert DOM event to jQuery event?
On Sep 22, 6:38 am, Liam Potter radioactiv...@gmail.com wrote: or this a href=# rel=foobarbazshow details/a $(a).click(function(){ var rel = $(this).attr('rel'); } now you have passed foobarbaz to jquery. This works only if I don't already need to use rel. I feel like there _must_ be a better way... Certainly this attribute abuse isn't a jQuery best practice? Walter
[jQuery] Re: Way to convert DOM event to jQuery event?
What about using the metadata plugin and storing data in the class attribute? A little hackish (in HTML5 can use the data attribute, which is more proper...): http://plugins.jquery.com/project/metadata In addition to rel, you could also use the title attribute on the anchor, no? Scott On Tue, Sep 22, 2009 at 4:47 PM, WalterGR walte...@gmail.com wrote: On Sep 22, 6:38 am, Liam Potter radioactiv...@gmail.com wrote: or this a href=# rel=foobarbazshow details/a $(a).click(function(){ var rel = $(this).attr('rel'); } now you have passed foobarbaz to jquery. This works only if I don't already need to use rel. I feel like there _must_ be a better way... Certainly this attribute abuse isn't a jQuery best practice? Walter
[jQuery] Re: Way to convert DOM event to jQuery event?
This doesn't make any sense. If you're already using jQuery why keep your handler in the HTML? I'll try to help anyway. a ... onclick=showTextNearThisLink(e, 'foobarbaz');.../a function showTextNearThisLink(e, sheez){ e = jQuery.event.fix(window.event || e); // e is your new jQueryized event }; -- ricardo On Sep 22, 5:47 pm, WalterGR walte...@gmail.com wrote: On Sep 22, 6:38 am, Liam Potter radioactiv...@gmail.com wrote: or this a href=# rel=foobarbazshow details/a $(a).click(function(){ var rel = $(this).attr('rel'); } now you have passed foobarbaz to jquery. This works only if I don't already need to use rel. I feel like there _must_ be a better way... Certainly this attribute abuse isn't a jQuery best practice? Walter
[jQuery] Re: Way to convert DOM event to jQuery event?
On Sep 22, 4:35 pm, Ricardo ricardob...@gmail.com wrote: This doesn't make any sense. If you're already using jQuery why keep your handler in the HTML? Because I need to pass additional information to the event handler in a clean way. (i.e. rather than hacks using various attributes on the anchor.) Is there a non-hackish way to do this? Thanks for the code snippet - that's what I was looking for. Walter
[jQuery] Re: Way to convert DOM event to jQuery event?
FYI, when I need to attach metadata to a non-empty element, I do it with an invisible child element. This results in some extra HTML, but I think that's as elegant as it can get until HTML5 becomes mainstream. style type=text/css .metadata { display: none; } /style a href=#show detailsspan class=metadatafoobarbaz/span/a $(a).click(function() { var metadata = $(this).children(.metadata).text(); } ); On Sep 22, 4:41 pm, WalterGR walte...@gmail.com wrote: On Sep 22, 4:35 pm, Ricardo ricardob...@gmail.com wrote: This doesn't make any sense. If you're already using jQuery why keep your handler in the HTML? Because I need to pass additional information to the event handler in a clean way. (i.e. rather than hacks using various attributes on the anchor.) Is there a non-hackish way to do this? Thanks for the code snippet - that's what I was looking for. Walter