I am looping through a hash using the .each method and then insert a new element into my page using values from the hash. I added a check to make sure I am not inserting the same element multiple times, but it seems to fail. Any ideas? items.each(function(item) { var pid = item[''pid'']; var stuff = item[''stuff'']; if( !$(pid) ) { // if DOM element with that id already exists, do not proceed <-- this seems to fail new Insertion.Top(''body'', ''<div id="''+pid+''">''+stuff+''</ div>'' ); } }); So in this case, in my hash I have {"pid":"142636","stuff":"some html"}, {"pid":"142636","stuff":"some more html"} and I want to only insert the first element, not both. But for some reason it inserts both --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Element ids cannot start start with a numerical value. That may be part of your problem. http://htmlhelp.com/reference/html40/attrs.html -B On Oct 19, 8:54 am, Gregory Stewart <gstewart...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am looping through a hash using the .each method and then insert a > new element into my page using values from the hash. I added a check > to make sure I am not inserting the same element multiple times, but > it seems to fail. Any ideas? > > items.each(function(item) { > var pid = item[''pid'']; > var stuff = item[''stuff'']; > > if( !$(pid) ) { // if DOM element with that id already exists, do > not proceed <-- this seems to fail > new Insertion.Top(''body'', ''<div id="''+pid+''">''+stuff+''</ > div>'' ); > } > > }); > > So in this case, in my hash I have > > {"pid":"142636","stuff":"some html"}, > {"pid":"142636","stuff":"some more html"} > > and I want to only insert the first element, not both. But for some > reason it inserts both--~--~---------~--~----~------------~-------~--~----~ 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''ve tried ''id_''+pid for my element id as well, same thing Thanks for the info though. Greg On Oct 19, 11:20 am, Bart Lewis <bartle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Element ids cannot start start with a numerical value. That may be > part of your problem. > > http://htmlhelp.com/reference/html40/attrs.html > > -B > > On Oct 19, 8:54 am, Gregory Stewart <gstewart...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am looping through a hash using the .each method and then insert a > > new element into my page using values from the hash. I added a check > > to make sure I am not inserting the same element multiple times, but > > it seems to fail. Any ideas? > > > items.each(function(item) { > > var pid = item[''pid'']; > > var stuff = item[''stuff'']; > > > if( !$(pid) ) { // if DOM element with that id already exists, do > > not proceed <-- this seems to fail > > new Insertion.Top(''body'', ''<div id="''+pid+''">''+stuff+''</ > > div>'' ); > > } > > > }); > > > So in this case, in my hash I have > > > {"pid":"142636","stuff":"some html"}, > > {"pid":"142636","stuff":"some more html"} > > > and I want to only insert the first element, not both. But for some > > reason it inserts both--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Also, I''d probably personally (out of preference) do that check with an array like this: var insertedElements = []; items.each(function(item) { var pid = item[''pid'']; var stuff = item[''stuff'']; if( insertedElements.indexOf(pid)==-1 ) { new Insertion.Top(''body'', ''<div id="''+pid+''">''+stuff+''</ div>'' ); insertedElements.push(pid) } }); http://prototypejs.org/api/array/indexof -B On Oct 19, 9:20 am, Bart Lewis <bartle...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Element ids cannot start start with a numerical value. That may be > part of your problem. > > http://htmlhelp.com/reference/html40/attrs.html > > -B > > On Oct 19, 8:54 am, Gregory Stewart <gstewart...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am looping through a hash using the .each method and then insert a > > new element into my page using values from the hash. I added a check > > to make sure I am not inserting the same element multiple times, but > > it seems to fail. Any ideas? > > > items.each(function(item) { > > var pid = item[''pid'']; > > var stuff = item[''stuff'']; > > > if( !$(pid) ) { // if DOM element with that id already exists, do > > not proceed <-- this seems to fail > > new Insertion.Top(''body'', ''<div id="''+pid+''">''+stuff+''</ > > div>'' ); > > } > > > }); > > > So in this case, in my hash I have > > > {"pid":"142636","stuff":"some html"}, > > {"pid":"142636","stuff":"some more html"} > > > and I want to only insert the first element, not both. But for some > > reason it inserts both--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---