Reputation: 45
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!
Upvotes: 1
Views: 82
Reputation: 59323
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
.
Upvotes: 0
Reputation: 56449
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 = "";
Upvotes: 3