1

So I have a list of users registered on my site in 1 column, in the 2nd is their email address with a checkbox next to it. On this page a user can check the box (or multiples) and click a submit button. Once they do that it will generate a list of the emails semicolon separated.

My issue is after they hit submit the lists generates, but the first email address has "undefined" written right next to it.. so instead of saying "[email protected]; [email protected]" it reads "[email protected]; [email protected]".

Here is my jQuery:

jQuery(document).ready(function() {
jQuery('#memberSubmit').click(function() {
    var emailList;
    jQuery('.email-members input:checked').each(function() {
        var $this = jQuery(this);
        emailList += $this.next('a').html() + "; ";
    });
    jQuery('.email-message').hide();
    jQuery('.email-members').hide();
    jQuery('.email-checks').hide();
    jQuery('#memberSubmit').hide();
    jQuery('.email-results a').attr('href', "mailto: " + emailList).fadeIn(2000);
    jQuery('.email-results .email-list p').html(emailList).fadeIn(2000);
    jQuery('.email-results h2').fadeIn(2000);
    jQuery('.email-results p').fadeIn(2000);
    jQuery('.email-list h2').fadeIn(2000);
    //console.info('Emails: ' + emailList);
});
});

I think my error is on the line: emailList += $this.next('a').html() + "; "; But I am not sure... any ideas?

Thanks!

1
  • Can you supply a short HTML example of your setup? Commented Jan 24, 2013 at 21:46

2 Answers 2

3

Initialize the emailList the variable first, that means it doesn't start at undefined when you perform your first go around. Coincidently, when you're calling += for the first time, it's actually converting undefined to a string, thus meaning your string always starting with that.

var emailList = "";
Sign up to request clarification or add additional context in comments.

2 Comments

Darn it you beat me by a couple of seconds :P
@Doorknob Hehe I noticed, sorry dude!
0

Try replacing emailList's declaration with this code:

var emailList = "";

That's because emailList starts out as undefined if you don't initialize it. Therefore undefined + "this is a test" would turn out as undefinedthis is a test.

1 Comment

Mention me in this answer tomorrow and I'll +1 you, I've hit the vote cap for today!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.