How to put a proper jQuery selector under if condition so that it work for all the cases? -


using jquery each function, parsing div data called myordersdiv

as parsing myordersdiv, creating array , storing data .

the issue facing that

if myordersdiv has got 1 elmenet under working fine , o/p

[{"name":"choclate"}]  

but in case if myordersdiv has got 2 elmenets under , o/p

[{"name":"chocolatevanila "}] (its mixing both of them )

the fiddle first case

http://jsfiddle.net/gstdu/3/

the fiddle second case

http://jsfiddle.net/gstdu/5/

right jquery selector is

$.each($('#myordersdiv > ul'), function(i, elem) {         name = $(elem).find("label").text();         if (name != 'undefined') {             //creating product array              products.push({                 'name': name             });         }     }); 

could please ??

try this:-

var name; var products = []; $('#myordersdiv > ul').find('ul').each(function(){     name = $(this).find("label").text();     if (name != 'undefined') {         //creating product array          products.push({             'name': name         });     }  });   var orderjson = json.stringify(products);  console.log(orderjson); 

demo


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -