Novice RoR programmer here, I''ve run into a bit of a stumbling block, concerning Time.now seemingly not updating in between JS function calls. My code is as follows: function test() { document.getElementById("chatbox").value += "<%= Time.now %>" + "\n"; } ... <textarea id="chatbox" rows="10" cols="80" class="chatbox" readonly="true" onclick="test();"> </textarea> Basically, clicking the textarea "chatbox" will add a line with the current time, and unfortunately, every click on the textarea results in the identical line being written, which is the time that the page was loaded. Is there any way I could get Time.now to update with every click? Thanks in advance! Nick.
On Nov 8, 3:36 pm, Nick S <nds...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> function test() > { > document.getElementById("chatbox").value += "<%= Time.now %>" + "\n"; > > } > > <textarea id="chatbox" rows="10" cols="80" class="chatbox" > readonly="true" onclick="test();"> > </textarea> > > Basically, clicking the textarea "chatbox" will add a line with the > current time, and unfortunately, every click on the textarea results > in the identical line being written, which is the time that the page > was loaded.That''s completely normal - At the point that the template is rendered all of your <%= are evaluated - if you look at the source in the browser you won''t see any of your ruby code (and even if you could, what''s your browser supposed to do with ruby). If you need some ruby code evaluated you need to make a request to the server Fred> > Is there any way I could get Time.now to update with every click? > > Thanks in advance! > > Nick.
Ahh I see, thank you very much for your help Frederick! On Nov 9, 12:57 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Nov 8, 3:36 pm, Nick S <nds...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: > > > function test() > > { > > document.getElementById("chatbox").value += "<%= Time.now %>" + "\n"; > > > } > > > <textarea id="chatbox" rows="10" cols="80" class="chatbox" > > readonly="true" onclick="test();"> > > </textarea> > > > Basically, clicking the textarea "chatbox" will add a line with the > > current time, and unfortunately, every click on the textarea results > > in the identical line being written, which is the time that the page > > was loaded. > > That''s completely normal - At the point that the template is rendered > all of your <%= are evaluated - if you look at the source in the > browser you won''t see any of your ruby code (and even if you could, > what''s your browser supposed to do with ruby). If you need some ruby > code evaluated you need to make a request to the server > > Fred > > > > > Is there any way I could get Time.now to update with every click? > > > Thanks in advance! > > > Nick. > >
Frederick Cheung wrote:> On Nov 8, 3:36�pm, Nick S <nds...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote: >> Basically, clicking the textarea "chatbox" will add a line with the >> current time, and unfortunately, every click on the textarea results >> in the identical line being written, which is the time that the page >> was loaded. > > That''s completely normal - At the point that the template is rendered > all of your <%= are evaluated - if you look at the source in the > browser you won''t see any of your ruby code (and even if you could, > what''s your browser supposed to do with ruby). If you need some ruby > code evaluated you need to make a request to the serverBut in this case, you probably don''t need a server request -- I think JavaScript has an equivalent to Time.now. In any case, I would advise against using ERb in JavaScript...> > FredBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.