A have an observer watch for a click on a link. Inside the triggered function, I want to extract a piece of information from the URL. Currently I am able to do: alert(Event.element(el)); This displays the url string of the link, which is exactly what I need to work with. However, when I try: Event.element(el).match(//); I get a javascript error "selector.match is not a function". I also note that prototype has an extension to Element named match as well. Am I accidentally using prototypes when I want the native javascript function? Or does Event.element() return a string to alert but something else otherwise? Is there any way to get the string displayed in the alert into a form I _can_ use .match(//) on? Confused again, Sean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Gilant wrote:> A have an observer watch for a click on a link. Inside the triggered > function, I want to extract a piece of information from the URL. > Currently I am able to do: > alert(Event.element(el)); > ... >I think you''re looking for location.href.match(...); it matches against the current url string. Event.element() returns an element object. - Ken Snyder --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Event.element returns an element. Alert-ing it performs an implicit toString(). There are two versions of match()--the one it seems you''re trying to use is a String method, which takes a regex as an argument. The other you correctly identified as a Prototype extension of Element. [1]. Using this version of match() accepts a CSS selector as a string, and returns a boolean. Event.element(evt).match(''form#someFormId input.someClass''); // returns boolean.. You could also do (less recommended) Event.element(evt).innerHTML.match(regex); Event.element(evt).inspect().match(regex); // [2] What you don''t say explicitly, but what I infer, is that you''re trying to get the value of some form element. var val = Event.element(evt).value; var val = $F(Event.element(evt)); // [3] TAG 1. http://prototypejs.org/api/element/match 2. http://prototypejs.org/api/element/inspect 3. http://prototypejs.org/api/utility/dollar-f 3b. http://prototypejs.org/api/form/element/getValue On Aug 2, 2007, at 3:22 PM, Gilant wrote:> > A have an observer watch for a click on a link. Inside the triggered > function, I want to extract a piece of information from the URL. > Currently I am able to do: > alert(Event.element(el)); > > This displays the url string of the link, which is exactly what I need > to work with. However, when I try: > Event.element(el).match(//); > > I get a javascript error "selector.match is not a function". I also > note that prototype has an extension to Element named match as well. > Am I accidentally using prototypes when I want the native javascript > function? Or does Event.element() return a string to alert but > something else otherwise? Is there any way to get the string displayed > in the alert into a form I _can_ use .match(//) on? > > Confused again, > Sean > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 3, 1:01 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> Event.element returns an element. Alert-ing it performs an implicit > toString().So, why not just call toString() explicitly if the implicit call returns the string desired? Indeed: Event.element(el).toString().match() Does exactly what I need, and the explicit call seems to properly disambiguate the use of .match to the one I was looking for. Moving on to find the next challenge in my learning curve! Many thanks, Sean --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 6, 2007, at 12:00 PM, Gilant wrote:> On Aug 3, 1:01 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: >> Event.element returns an element. Alert-ing it performs an implicit >> toString(). > > So, why not just call toString() explicitly if the implicit call > returns the string desired? Indeed: > Event.element(el).toString().match()1. Because that implementation is fragile: it depends on a browser- specific implementation of an element''s toString method. 2. Because there are easier, faster, more descriptive, and more legible ways to do the same thing. The standard DOM methods and Prototype''s helper methods are good here. Examples: // Testing the element is an element node, not a text node: $(el).nodeType = 1; // [1] // Test if element is tr $(el).match(''tr''); // [2] $(el).nodeName == "TR"; // [3] // Test if element is tr with class "alternateRow" $(el).match(''tr.alternateRow''); If the explicit toString() call followed by a regex works, that''s great. I''ll still assert there are better was of doing it, but it''s your app, so do as you like. I''m just offering what I believe is a more elegant solution. TAG 1. http://zytrax.com/tech/dom/nodetype.html 2. http://prototypejs.org/api/element/match 3. http://www.javascriptkit.com/domref/nodetype.shtml (part way down) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I appreciate your trying to offer more elegant solutions! However I don''t see how the solution you suggest in any way applies to my problem. In your examples you are using prototype''s match() to test classes. That is of no help to me whatsoever with the problem I tried to outline in my OP. Let me try to be more descriptive. 1) I have a link in the page, and the contents of the href attribute of that link contains information I need. For example: <a href="#key" id="some_id">Foo</a> 2) The <a> elements id is already known to me, but the ''key'' in href="#key" is not. This key is essential for fetching something from the server (using AJAX) and updating the page. 3) Using prototype I''ve added, on page load, an observer to watch for clicks on that link. When clicked, I want to get the key from the href and use that in making the AJAX request. Currently, following your toString() hint from an earlier post, I am using the following to extract the key from the href: $(''some_id'').observe(''click'', function (el) { var key = Event.element(el).toString().match(/regex/)[1]; // better solution for this? new Ajax.Request(Event.element(el), { ... }); // using key in there }); Which is working under Firefox 2 and IE 7. If there is a more robust solution to get the string value of the href such that I can check it with a regular expression and extract the key, I would certainly be open to giving it a try. Thanks again, Sean On Aug 6, 3:55 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> On Aug 6, 2007, at 12:00 PM, Gilant wrote: > > > On Aug 3, 1:01 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: > >> Event.element returns an element. Alert-ing it performs an implicit > >> toString(). > > > So, why not just call toString() explicitly if the implicit call > > returns the string desired? Indeed: > > Event.element(el).toString().match() > > 1. Because that implementation is fragile: it depends on a browser- > specific implementation of an element''s toString method. > > 2. Because there are easier, faster, more descriptive, and more > legible ways to do the same thing. The standard DOM methods and > Prototype''s helper methods are good here. > > Examples: > > // Testing the element is an element node, not a text node: > $(el).nodeType = 1; // [1] > > // Test if element is tr > $(el).match(''tr''); // [2] > $(el).nodeName == "TR"; // [3] > > // Test if element is tr with class "alternateRow" > $(el).match(''tr.alternateRow''); > > If the explicit toString() call followed by a regex works, that''s > great. I''ll still assert there are better was of doing it, but it''s > your app, so do as you like. I''m just offering what I believe is a > more elegant solution. > > TAG > > 1.http://zytrax.com/tech/dom/nodetype.html > 2.http://prototypejs.org/api/element/match > 3.http://www.javascriptkit.com/domref/nodetype.shtml (part way down)--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
I see. IIRC you didn''t fully explain your use case, so I''ve been shooting in the dark about what you''re looking for. With regard to what you''re trying to do, it sounds like a regex match is the way to go--I''d simply suggest referencing the href property directly, rather than toString() on the element: el.href.match(regex); TAG On Aug 6, 2007, at 3:04 PM, Gilant wrote:> > I appreciate your trying to offer more elegant solutions! However I > don''t see how the solution you suggest in any way applies to my > problem. In your examples you are using prototype''s match() to test > classes. That is of no help to me whatsoever with the problem I tried > to outline in my OP. Let me try to be more descriptive. > > 1) I have a link in the page, and the contents of the href attribute > of that link contains information I need. For example: > <a href="#key" id="some_id">Foo</a> > > 2) The <a> elements id is already known to me, but the ''key'' in > href="#key" is not. This key is essential for fetching something from > the server (using AJAX) and updating the page. > > 3) Using prototype I''ve added, on page load, an observer to watch for > clicks on that link. When clicked, I want to get the key from the href > and use that in making the AJAX request. Currently, following your > toString() hint from an earlier post, I am using the following to > extract the key from the href: > $(''some_id'').observe(''click'', function (el) { > var key = Event.element(el).toString().match(/regex/)[1]; // > better solution for this? > new Ajax.Request(Event.element(el), { ... }); // using key in > there > }); > > Which is working under Firefox 2 and IE 7. If there is a more robust > solution to get the string value of the href such that I can check it > with a regular expression and extract the key, I would certainly be > open to giving it a try. > > Thanks again, > Sean > > On Aug 6, 3:55 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: >> On Aug 6, 2007, at 12:00 PM, Gilant wrote: >> >>> On Aug 3, 1:01 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote: >>>> Event.element returns an element. Alert-ing it performs an >>>> implicit >>>> toString(). >> >>> So, why not just call toString() explicitly if the implicit call >>> returns the string desired? Indeed: >>> Event.element(el).toString().match() >> >> 1. Because that implementation is fragile: it depends on a browser- >> specific implementation of an element''s toString method. >> >> 2. Because there are easier, faster, more descriptive, and more >> legible ways to do the same thing. The standard DOM methods and >> Prototype''s helper methods are good here. >> >> Examples: >> >> // Testing the element is an element node, not a text node: >> $(el).nodeType = 1; // [1] >> >> // Test if element is tr >> $(el).match(''tr''); // [2] >> $(el).nodeName == "TR"; // [3] >> >> // Test if element is tr with class "alternateRow" >> $(el).match(''tr.alternateRow''); >> >> If the explicit toString() call followed by a regex works, that''s >> great. I''ll still assert there are better was of doing it, but it''s >> your app, so do as you like. I''m just offering what I believe is a >> more elegant solution. >> >> TAG >> >> 1.http://zytrax.com/tech/dom/nodetype.html >> 2.http://prototypejs.org/api/element/match >> 3.http://www.javascriptkit.com/domref/nodetype.shtml (part way down) > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 6, 5:15 pm, Tom Gregory <t...-PGZyUNKar/Q@public.gmane.org> wrote:> el.href.match(regex);LMAO! Right, forest for the trees and all that. Your patience is appreciated. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---