yes I know they are two different things, I do know both JSP and
JavaScript, so I do know the difference; I would like to know why what
works in JSP doesn't work in JavaScript..  again:

this works in JSP:

<%  for (int i = 1; i < 5; i++) { %>
  $('#image<%=i%>').bind('mouseover',function(event) {
        $('#image<%=i%>').addClass('dim');
    });
<% } %>

but the same thing, in JavaScript, doesn't work:

for (i = 1; i < 5; i++) {
  $('#thumb' + i).bind('mouseover',function(event) {
     $('#thumb' + i).addClass('dim');
  });
}

in JavaScript this code is applied ONLY when i=5,  in JSP it applies
to all four elements processed in the array and works as expected....

ok, to make it easier, forget the comparison to JSP...   why does
above JavaScript code not work?? I create four elements dynamically,
but event-binding does not work for any of the four elements...

thank you...



On Aug 28, 6:56 pm, James <james.gp....@gmail.com> wrote:
> I have difficulties understanding what you're trying to achieve.
> As someone mentioned, Java is to Javascript as Ham is to Hamster.
> They're two different beasts.
>
> For the 'elem+1' situation, you can do:
>     this['elem'+1] = '...';
> to dynamically create variables. But usually you don't have to resort
> to doing something like that if you code things properly.
>
> On Aug 28, 12:44 pm, kali <maya778...@yahoo.com> wrote:
>
> > hi,
>
> > I have a problem with binding events to dynamically-generated
> > elements; specif. problem is something I'm trying to do with
> > JavaScript doesn't work but it works if I do it inJSP:   (and can we
> > PLEASE get ability to display CODE in this forum in NON-PROPORTIONAL
> > FONT???)
>
> >JSP, event-binding works:
> > ------------------------
>
> >         <%  for (int i = 1; i < 5; i++) { %>
> >                 var elem<%=i%> = $('<a id="link<%=i%>"><img id="image<%=i
> > %>" /></a>');
>
> >                 $('#image<%=i%>').bind('mouseover',function(event) {
> >                                 $('#image<%=i%>').addClass('dim');
> >                 });
> >         }
>
> > JavaScript, event-binding does not work:
> > ---------------------------------------
>
> > for (i = 1; i < 5; i++) {
>
> >   var elem = $('<a id="link<%=i%>"><img id="image<%=i%>" /
>
> > ></a>');
>
> >   $('#thumb' + i).bind('mouseover',function(event) {
> >      $('#thumb' + i).addClass('dim');
> >   });
>
> > }
>
> > (also: this declaration doesn't work in JS:
>
> > var elem+i = $('<a id="link<%=i%>"><img id="image<%=i%>" /></a>');
>
> >     ^^^^^^
>
> > 'elem+i' returns a syntax error; why does this declaration 'var elem<
> > %=i%>' work inJSPbut equivalent does not in JS?
>
> > urls (JSPand JS) are here:
>
> >http://www.mayacove.com/dev/jquery/events/test_event_dyn.htmlhttp://w...
>
> > thank you very much..

Reply via email to