-1

I am new to jQuery and especially to arrays and am trying to create an unordered list from an array. The array contains all unique values from certain parts of a table and creating this works as intended (tested via alert) but when I try to create a list with these values nothing (visible) happens.

Can someone tell me what I am doing wrong here ? Also, is there a way I can cover the case that the array does not contain any values and then just show a dummy or default item in the list ?

Note: '#test' is a test div where I was playing with this.

My jQuery:

$('#tblLegend td.col1').on('click', function(){
    var tblValues = new Array();
    $('#tblCalendar').find('tbody > div.editable').each(function(){
        if(tblValues.indexOf($(this).text()) == -1){
            tblValues.push($.trim($(this).text()));
        }
    });
    var ul = '<ul>';
        $.each(tblValues, function(i){
            ul+= '<li>' + tblValues[i] + '</li>';
        });
    ul+= '</ul>';
    $('#test').html(ul);
});

Many thanks in advance, Mike

4
  • 3
    There no obvious wrong code. Commented Jun 3, 2015 at 12:37
  • 2
    can you replicate the problem in a fiddle? Commented Jun 3, 2015 at 12:38
  • Thanks - I'll have a look. Commented Jun 3, 2015 at 12:39
  • Ok, I added a few list items manually and removed the $.each part for testing and it then creates and displays the list properly but not when I create it from the array like above. Unfortunately I cant create a fiddle from this as there are too many other things on the table here. Are you sure there is no error in the code above ? Commented Jun 3, 2015 at 12:44

1 Answer 1

0
$('#tblCalendar').find('tbody > div.editable')

Are you sure your selector ? normally, the TBODY (body of table) dont may directly contain a DIV.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot - that fixed it ! :) You were right, my divs are actually within the tds so changing the selector to 'tbody td div.editable' covered this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.