Bruc Walker
Bruc Walker

Reputation: 253

The eq fliter is not working

Why this code is not working? Meaning nothing get displayed into the ul tag.

$("#server").each(function()
{
  var textValue = $(this).text();

  $('#list li').eq(counter).append('<li>'  + textValue + '</li>');      // <-- not working
  $('#list li').eq(counter).insertAfter('<li>'  + textValue + '</li>'); // <-- or not working
  $('#list')append('<li>'  + textValue + '</li>');                      // <-- or working
});

<div>
     <ul id="list"></ul>
</div>

Upvotes: 0

Views: 78

Answers (3)

jfriend00
jfriend00

Reputation: 707696

I think your full answer is here in your previous question on this topic (read the comments to your questions in my answer).

I think you want jQuery's method .after() instead of .insertAfter(). Full working example including a jsFiddle here.

$('#list li').eq(counter).after('<li>'  + textValue + '</li>');

Upvotes: 0

digitalbreed
digitalbreed

Reputation: 4070

I see two problems.

  1. $('#list li') doesn't resolve to anything since there is no <li> yet. It should be ul instead.
  2. If you're looking at a specific index with eq during iteration, you may want to pass in the current index into the functor, like so: $('#server').each(function(counter){... - but maybe counter comes from somewhere else, that's not clear from your posting.

Regards db

Upvotes: 0

Daniel Brockman
Daniel Brockman

Reputation: 19290

  1. #server can only match a single element.
  2. counter is undefined.
  3. #list does not contain any items, so #list li will not match anything.
  4. You are missing a period before append.

Upvotes: 2

Related Questions