You're pushing the value from each div into a generic array instead of
the "elements" array, and adding a comma after each item as if you are
concatenating a string. Also, you have a dollar sign in front of your
alert().

Try something like this instead:

$('.button').click(function(){
        var elements = new Array();

        $('.elementSelected').each(function(){
                elements.push($(this).html());
        });

        alert(elements);
});

I would also recommend using the HTML "button" element for your button
instead of a generic div, i.e.:

<button>Click Me</button>

...instead of:

<div>Click Me</div>


- jason



On Apr 3, 7:45 am, hedgomatic <[EMAIL PROTECTED]> wrote:
> I'm trying to reference an element in an each() that's inside an event
> function.
>
> scenario--
> I have a div with a class of .button, several divs with the
> class .elementSelected.
> I click on the div with the .button class, and then...
>
> $(".button").click(function() {
>                 elements = new Array();
>                 $(".elementSelected").each(function() {
>                         var wordsSelected = $(this).html(); // this is 
> probably wrong.
>                         Array.push(wordsSelected+',');
>                         $alert(elements); //debug
>
>                 });
>         });
>
> the alert doesn't come up as undefined or null, it just comes up
> completely blank. Which is odd because even if $(this) is still
> referring to the div that was clicked with the .button class, it has
> innerHTML that could be returned. If $(this) does change scope in the
> each() loop, then that also has innerHTML that could be returned.
>
> is this just not possible, or is there a different syntax for
> addressing this sort of thing?

Reply via email to