0

Don't worry about the vwo__$, it is just jquery used on another system.

First I am creating id's numbered for each, then instead of repeating the code can I make jquery find all id's div.text-right#top-1 up to around 20?

vwo_$.each(vwo_$("div.details div.text-right"), function(ind) {
   vwo_$(this).attr('id', 'top-' + parseInt(ind + 1));
});

var x = 1
vwo_$.each(vwo_$("div.text-right#top-:eq(' + x + ')"), function(x) {
   x++;
   vwo_$(this).append(vwo_$("div.infolist ul:nth-child(1) li:nth-child(1)"));
   console.log(x)
   });

I would like a more efficient for the jquery selector to count.

It doesn't continue looking for ids after #top-1

artworkjpm
  • 1,255
  • 2
  • 19
  • 40
  • 5
    Possible duplicate of [How to get CSS to select ID that begins with a string (not in Javascript)?](https://stackoverflow.com/questions/11496645/how-to-get-css-to-select-id-that-begins-with-a-string-not-in-javascript) – silentw Dec 11 '17 at 16:02
  • 2
    `[id^=top-]` css selector. – silentw Dec 11 '17 at 16:03

1 Answers1

0
vwo_$.each(vwo_$("div.details div.text-right"), function(ind) {
    vwo_$(this).attr('id', 'top-' + parseInt(ind + 1));
});

vwo_$.each(vwo_$("div.text-right[id^=top-]"), function(idx, value) {
    vwo_$(this).append(vwo_$("div.infolist ul:nth-child(1) li:nth-child(1)"));
    console.log(idx);
});

Edit:

[id^=top-] ^= indicates "starts with". Conversely, $= indicates "ends with".

The symbols are actually borrowed from Regex syntax, where ^ and $ mean "start of string" and "end of string" respectively.

See the specs for full information.

SOURCE

silentw
  • 4,835
  • 4
  • 25
  • 45