Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6361849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:49:48+00:00 2026-05-24T23:49:48+00:00

I understand closure somewhat, but sometimes I can’t seem to get it right. I

  • 0

I understand closure somewhat, but sometimes I can’t seem to get it right. I believe this to be the culprit here… Since i’m traversing tho, i’m not sure if closure is necessary or possible.

So I have to parse some external markup that looks like this:

<h2>Fried Calamari</h2>
<dd>Price:$8.95</dd>
<dd>Pan-fried calamari served with our delicious homemade tomato sauce.</dd>
<h3>Extra Sauce</h3>
<dd>Required:0 / Up To:99</dd>
<ol>
<li><dt>Extra Sauce</dt><dd>Price:$0.99</dd></li>
</ol>
<h2>Fried Zuchinni</h2>
<dd>Price:$6.95</dd>
<dd>Tossed with garlic and herbs. Served with a side of tomato sauce.</dd>
<h3>Extra Sauce</h3>
<dd>Required:0 / Up To:99</dd>
<ol>
<li><dt>Extra Sauce</dt><dd>Price:$0.99</dd></li>
</ol>
<h2>Eggplant Rollatini Appetizer</h2>
<dd>Price:$7.95</dd>
<dd>Fresh rolled eggplant filled with Ricotta and topped with tomato sauce and melted Mozzarella.</dd>
<h3>Extra Sauce</h3>
<dd>Required:0 / Up To:99</dd>
<ol>
<li><dt>Extra Sauce</dt><dd>Price:$0.99</dd></li>
</ol>
<h2>Mozzarella Sticks</h2>
<dd>Price:$6.95</dd>
<dd>Classic Mozzarella sticks served with a side of tomato sauce.</dd>
<h3>Extra Sauce</h3>
<dd>Required:0 / Up To:99</dd>
<ol>
<li><dt>Extra Sauce</dt><dd>Price:$0.99</dd></li>
</ol>
<h2>Buffalo Rock Shrimp</h2>
<dd>Price:$9.75</dd>
<dd>Crispy rock shrimp toasted in our signature buffalo sauce. Served with Blue cheese crumbles.</dd>
<h3>Extra Dressing</h3>
<dd>Required:0 / Up To:99</dd>
<ol>
<li><dt>Extra Dressing</dt><dd>Price:$0.99</dd></li>
</ol>

Here’s my js using jQuery…

$(document).ready(function(){

 $('h2').each(function(i) {
     //to hold the info
     var itemDetail = new Object();
     //h2's contain the name
     itemDetail['name'] = $(this).html();       

     // but dd's contain price description and other
     // and we need to traverse all <dd> tags for each <h2> 
     $(this).siblings('dd').each(function(){             
         var itemInfo = $(this).html();
         var priceLabel = itemInfo.slice(0,7);
         var priceValue = itemInfo.slice(7);
         if (priceLabel='Price:$'){
             itemDetail['price'] = priceValue;
         }else{
             // discard required info (leaving description)
             if (itemInfo.slice(0,9) != 'Required:')
             //descriptions don't have a label 
             itemDetail['description'] = itemInfo;
         }
     });
     console.log(itemDetail)
 }); 
}); 

if i step thru the traversals using alert() on priceLabel and priceValue, the logic echo’s the correct values (specifically priceValue is a numberwhen priceLabel matches the criteria, but when I log the object, i’m not getting info for each iteration of the inner traversal. Instead priceLabel is equal to the .slice() of the final iteration.

Is there a better way to avoid this or use closure to force info into my objects…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:49:48+00:00Added an answer on May 24, 2026 at 11:49 pm

    A couple of issues:

    • The = does assignment but you used it for comparison instead of using == or ===
    • You used .siblings('dd') which is giving you all the dd elements, instead of just the next adjacent ones. With your current markup, you can use nextUntil(':not(dd)') instead.
     $('h2').each(function(i) {
         var itemDetail = new Object();
         itemDetail['name'] = $(this).html();       
    
           // ----v---get next siblings of "h2" until on that is not "dd" is reached
         $(this).nextUntil(':not(dd)').each(function(){             
             var itemInfo = $(this).html();
             var priceLabel = itemInfo.slice(0,7);
             var priceValue = itemInfo.slice(7);
    
                  //--------v----------- use == instead of =
             if (priceLabel == 'Price:$'){
                 itemDetail['price'] = priceValue;
             }else{
                 if (itemInfo.slice(0,9) != 'Required:')
                 itemDetail['description'] = itemInfo;
             }
         });
         console.log(itemDetail)
     }); 
    

    EDIT:

    If your markup is consistent the way it shows, then you can shorten your code a bit like this:

     $('h2').each(function(i) {
         var itemDetail = new Object();
         itemDetail['name'] = $(this).html();       
    
         var dds = $(this).nextUntil(":not(dd)");
         itemDetail['price'] = dds.eq(0).html().slice(7);
         itemDetail['description'] = dds.eq(1).html();
         console.log(itemDetail);
     }); 
    

    Or even shorter using an object literal:

     $('h2').each(function(i) {
         var dds = $(this).nextUntil(":not(dd)");
         var itemDetail = {
                    name: $(this).html(),
                   price: dds.eq(0).html().slice(7),
             description: dds.eq(1).html()
         };
         console.log(itemDetail);
     }); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand what System.WeakReference does, but what I can't seem to grasp is a
Can any one please describe this sort of code to understand Java closure. public
I can't understand how to get the google closure compiler for javascript to properly
The complete code can be found here: http://www.webdeveloper.com/forum/showthread.php?t=224180#6 This emulates jquery like functionality. The
It maybe sleep deprivation but I cannot understand why this isn't working. I want
I've been reading a lot about closures and I think I understand them, but
I understand how I can change the dns settings for my domains by editing
I think I have this figured out, but am hoping to cleary have this
right, basically we have a link into our system that is all lowercase but
I understand what closures are, but I am having some trouble grokking exactly what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.