SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    790
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    jQuery: pls help with generating nested lists....

    what I need to generate
    (inserting this into existing empty <ul>, so need to start by inserting top-level <li>):

    Code:
      <li>camera
            <ul>
                <li>price: $890.00</li>
                <li>brand: Nikon</li>
                <li>model: D700</li>
            </ul>
        </li>
        <li>laptop
            <ul>
                <li>price: $980.00</li>
                <li>brand: Toshiba</li>
                <li>model: L989</li>
            </ul>
        </li>
       etc.....

    for some reason, this code

    Code:
    $.each(prod, function(propKey,propVal) {
    	
    	$('ul#prodsAll').append('<li>' + propKey + ':<ul>');
    	
    		$.each(propVal, function(propKey2,propVal2) {
    			$('#prodsAll').append('<li>' + propKey2 + ': ' + propVal2  + '</li>');
    		});
    		$('#prodsAll ul').append('</ul></li>');
    	});
    });
    is generating this:

    Code:
    <ul id="prodsAll">
    	<li>
    	camera:
    	<ul></ul>
    	</li>
    	<li>price: $890.00</li>
    	<li>brand: Nikon</li>
    	<li>model: D700</li>
    	<li>...</li>
    	<li>...</li>
    	<li>...</li>
    </ul>

    would appreciate suggestions... thank you...

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi Maya,

    Seems like you are reading your values from some JSON or something.
    Could you supply that, please.

    E.g. it would help to know what prod is in this line:

    Code:
    $.each(prod, function(propKey,propVal) {

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    790
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    yes I'm reading from JSON...

    but well, I found solution:

    Code:
    var content = '';
    $.each(data.products, function(index,prod) {
    	
    	$.each(prod, function(propKey,propVal) {
    		
    		content += '<li>' + propKey + ':<ul>';
    		
    		$.each(propVal, function(propKey2,propVal2) {
    			content += '<li>' + propKey2 + ': ' + propVal2  + '</li>';
    		});
    		content += '</ul></li>';
    	});
    });
    $('ul#prodsAll').append(content);

    it was not a question of whether data was coming from JSON or not.. I was just not doing it right...;-)

    thank you....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •