0

I have a variable I have generated (with help from a previous question/answer) that looks like this...

var ids = $(".myelement").map(function() {
  return '#' + $(this).closest(".panel").prop("id");
}).get().join(', ');

console.log(ids);

The output returned in the console log is...

#element1, #element2, #element3

I am now trying to use this variable as an option when initializing a jquery plugin like this...

$.scrollify({
    section : ids,
});

This is not working and is just returning 'ids' instead of the value of ids. Am I doing it incorrectly or does the ability to accept the variable depend on the jQuery plugin itself?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • 1
    Does it work if you do : `$.scrollify({ section : #element1,#element2,#element3" });` – freedomn-m Sep 24 '18 at 11:35
  • Yep, works correctly when I enter them manually. Looking at the code I can see it just prints 'ids' instead of the value of the variable – fightstarr20 Sep 24 '18 at 11:37
  • 1
    Had a quick look at jquery-scrollify and it correcltly does `$(selector).each` – freedomn-m Sep 24 '18 at 11:38
  • 1
    There's nothing obvious with the code provided - could it be the order that it's run? Hard to tell when separate snippets. eg is there an ajax call? – freedomn-m Sep 24 '18 at 11:39
  • I'm wondering if it could be as simple as a scope issue? – fightstarr20 Sep 24 '18 at 11:41
  • 1
    Should work https://jsfiddle.net/z_acharki/ge034w1m/81/ – Zakaria Acharki Sep 24 '18 at 11:48
  • 1
    If `var ids` is not inside the same scope, then yes, it would fail. Just before your `$.scrollify({` line, add `alert(ids);` (or `console.log`) to see what it is at that point (or use a debugger ofc). Without seeing more code, can't tell if that's the issue, but sounds likely. – freedomn-m Sep 24 '18 at 12:00

0 Answers0