1

I want to replace $('#divy a:lt(3)') and add the variable instead of number $('#divy a:lt(count)'), but it's not working.

Fiddle

var timerId,
count = 0;

function end_counter() {
    $('#divy a:lt(3)').each(function () {
        var $this = $(this);
        $this.attr('target', '_blank');
        $this.get(0).click(function () {
            window.open(this);
            return false;
        });
    });
    count = 0;
}
$('button').click(function () {

    count++;
    clearTimeout(timerId);
    timerId = setTimeout(end_counter, 700);
});
nbrooks
  • 18,126
  • 5
  • 54
  • 66
neobian
  • 41
  • 2
  • What is your overall goal? What exactly are you trying to accomplish with this code? Telling us that you want to replace `$('#divy a:lt(3)')` doesn't help, as we don't know your ultimate goal. – royhowie Jul 18 '14 at 03:12
  • `get(0)` returns a `DOM` object, not a `jQuery` object, but also, since `$this` is the subject of an `.each`, no reason to use `get` since `$this` is only a single element anyway. – chiliNUT Jul 18 '14 at 03:13
  • When the 3 is there it will open up the first 3 hrefs in the div, by adding in a variable name instead of the number I want to change the number of hrefs that are open, eg: count= 5, open the first 5 pages. But when a add the variable name rather than the number it doesnt work. – neobian Jul 18 '14 at 03:17

2 Answers2

3

The problem is that your jQuery selector is the string $("#divy a:lt(count)"). What you need to do is change that string depending on the content of count. Changing that to $("#divy a:lt("+count+")") should fix it for you. That way if count is 5 your selector is $("#divy a:lt(5)")

1

You need to have something like this to work

$('#divy a:lt('+count+')')
V31
  • 7,626
  • 3
  • 26
  • 44