-1

I am trying to put a variable inside a jQuery selector. However I am assuming that I am breaking some form of a syntax rule. Please let me know if you can spot the mistake:

var names = ["JakeP97", "Trishy", "Puffs", "Evilgenious", "Joeyc", "TheKid"];
var ballots = ["#book1", "#book2", "#book3", "#book4", "#book5", "#book6"];

function splitName(plName,ballotNum) {
    var halfplName = Math.round(plName.length / 2);
    var firstplname = plName.substr(0, halfplName);
    var lastplName = plName.substr(halfplName, plName.length);
    $(ballotNum 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
    $(ballotNum 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname);
}

for (i=0; i<ballots.length; i++) {
    splitName(names[i],ballots[i]);
}

Error takes place on the following lines: $(ballotNum 'ul.hardcover_front')

The desired result would be: $('#book1 ul.hardcover_front'), $('#book2 ul.hardcover_front') etc, all the way.

Thank you in advance!

dda
  • 6,030
  • 2
  • 25
  • 34
evilgenious448
  • 518
  • 2
  • 11
  • It's $(ballotNum + 'ul.hardcover_front'), you're missing a + to join a string. also make sure ballotNum has a hash at the front. – Adrian Sep 05 '17 at 09:01

3 Answers3

4

You need to concat the string and variables.

$(ballotNum + ' ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
Jurij Jazdanov
  • 1,248
  • 8
  • 11
1

You are missing the + sign. Since you are concatenating the variable with a string, you need to use + sign:

$(ballotNum + 'ul.hardcover_front')

So this lines:

$(ballotNum 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
$(ballotNum 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname);

will change to:

$(ballotNum + 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
$(ballotNum + 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname);
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
1
$(ballotNum + 'ul.hardcover_front').find('li:nth-child(2)').html(firstplname);
$(ballotNum + 'ul.hardcover_back').find('li:nth-child(1)').html(lastplname);

I think you forgot adding +.

kyun
  • 9,710
  • 9
  • 31
  • 66