0

I have this product list that I'm trying to populate using jQuery.

Since doing it solely on HTML, it'll take a lot of work and a very, very long line of codes for each item. Here's my sample HTML code.

<li>
  <div class="col-md-4">
    <div class="acs-detail">
      <legend>Side Visor</legend>
      <img src="img/side_visor.jpg" class="veh-content img-responsive" />
      <div class="pna-description col-md-12">
        <ul>
          <li>Keeps the rain out and fresh air in with these durable side visors.</li>
        </ul>
      </div>
      <div class="pna-price col-sm-6 col-md-12">
        <div class="text-deco">
          <span>P 700 <a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."><sup>[*]</sup></a></span>
        </div>
        <div class="border">
          <span></span>
        </div>
      </div>
    </div>
  </div>
</li>

What I need to populate is:

  • <legend>
  • <img src= "">
  • .pna-description ul li
  • span
2
  • Would you be able to add the code for your array? Commented Oct 14, 2017 at 3:42
  • had little idea on how to. but really needs help Commented Oct 14, 2017 at 6:42

3 Answers 3

2

I would use something like this:

array.forEach(function (current) {
  var legend     = current[0],
      imgSrc     = current[1],
      descript   = current[2],
      price      = current[3];
  $('ul.the-list').append(`
    <li>
      <div class="col-md-4">
        <div class="acs-detail">
          <legend>` + legend + `</legend>
          <img src="` + imgSrc + `" class="veh-content img-responsive" />
          <div class="pna-description col-md-12">
            <ul>
              <li>` + descript + `</li>
            </ul>
          </div>
          <div class="pna-price col-sm-6 col-md-12">
            <div class="text-deco">
              <span>` + price + `</span>
            </div>
            <div class="border">
              <span></span>
            </div>
          </div>
        </div>
      </div>
    </li>
  `);
});

With the array of something like this:

array = [
      [
      'Side Visor',
      'img/side_visor.jpg',
      'Keeps the rain out and fresh air in with these durable side visors.',
      'P 700 <a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."><sup>[*]</sup></a>',
      ],
      //more items
 ]

array = [
  [
  'Side Visor',
  'img/side_visor.jpg',
  'Keeps the rain out and fresh air in with these durable side visors.',
  'P 700 <a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."><sup>[*]</sup></a>',
  ],
  [
  'Side Visor',
  'img/side_visor.jpg',
  'Keeps the rain out and fresh air in with these durable side visors.',
  'P 700 <a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."><sup>[*]</sup></a>',
  ],
  [
  'Side Visor',
  'img/side_visor.jpg',
  'Keeps the rain out and fresh air in with these durable side visors.',
  'P 700 <a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."><sup>[*]</sup></a>',
  ],
]

array.forEach(function (current) {
  var legend     = current[0],
      imgSrc     = current[1],
      descript   = current[2],
      price      = current[3];
  $('ul.the-list').append(`
    <li>
      <div class="col-md-4">
        <div class="acs-detail">
          <legend>` + legend + `</legend>
          <img src="` + imgSrc + `" class="veh-content img-responsive" />
          <div class="pna-description col-md-12">
            <ul>
              <li>` + descript + `</li>
            </ul>
          </div>
          <div class="pna-price col-sm-6 col-md-12">
            <div class="text-deco">
              <span>` + price + `</span>
            </div>
            <div class="border">
              <span></span>
            </div>
          </div>
        </div>
      </div>
    </li>
  `);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class='the-list'>
</ul>

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

5 Comments

how will i call this in html?
Sorry, was away. Glad to know it's working! Your welcome! God bless :)
last question , will it still be alot faster to load all of the items that i will be putting on jquery. comparing to a hard html code ?
Honestly, I would form the HTML page using jQuery, then copy the HTML and paste it into a file.
i have 0 idea on what you're talking about! hehe but see what i can do ! thanks.
2

Not sure if this is what you mean :

const datas = [{
    legend: "Side Visor",
    imgSrc: "img/side_visor.jpg",
    liContent: "Keeps the rain out and fresh air in with these durable side visors.",
    spanContent: "P 700 ",
    dataContent: "All prices listed herein are Manufacturer’s Suggested Retail Prices (MSRP), which exclude installation, and are subject to change without prior notice. Dealer sets actual price. Pictures are for informational purposes only."
}];
$('#yourNode').append(datas.map(data => `
    <li>
        <div class="col-md-4">
            <div class="acs-detail">
                <legend>${data.legend}</legend>
                <img src="${data.imgSrc}" class="veh-content img-responsive" />
                <div class="pna-description col-md-12">
                    <ul>
                        <li>${data.liContent}</li>
                    </ul>
                </div>
                <div class="pna-price col-sm-6 col-md-12">
                    <div class="text-deco">
                        <span>${data.spanContent}<a   data-placement="top"  data-toggle="popover" data-trigger="hover" data-content="${data.dataContent}"><sup>[*]</sup></a></span>
                    </div>
                    <div class="border">
                        <span></span>
                    </div>
                </div>
            </div>
        </div>
    </li>
`).join(''))

Online : https://codepen.io/cyl19910101/pen/EwedEZ?editors=0011

Comments

1

I would suggest a template approach: create a template, and then clone that template for every item in your list. The strategy I outline below places the template as a literal HTML snippet somewhere in the actual page, but tells the browser (through CSS) to never display the template. (But the template could just as well be a string in Javascript, or pulled in via Ajax, or ...)

The CSS:

.template { display:none !important; }

The HTML:

<!-- HTML snippet, placed wherever you need it -->
<li class="template" id="ProductTemplateRow">
    <div class="col-md-4">
        <div class="acs-detail">
            <legend></legend>
            <img class="veh-content img-responsive" />
            <div class="pna-description col-md-12">
                <ul><li></li></ul>
            </div>
            <div class="pna-price col-sm-6 col-md-12">
                <div class="text-deco">
                    <span></span>
                </div>
                <div class="border">
                    <span></span>
                </div>
            </div>
        </div>
    </div>
</li>

Finally the JS/jQuery code that utilizes the above template:

var $tmpl, $prodRow, i, p, products;

// clone what's on the page so we don't change our template for other
// processing we may need to do
$tmpl = $("#ProductTemplateRow").clone();

// remove the template specific characteristics
$tmpl.removeClass("template").removeAttr("id");

products = [];  // an array of <li> elements, which we append below
for ( i = 0; i < productList.length; ++i ) {
    p = productList[ i ];
    $prodRow = $tmpl.clone();  // create a new product HTML row from the template

    // find the appropriate items in the new product row, and populate them
    $prodRow.find('legend').html( p.legend );
    $prodRow.find('img').attr('src', p.imgSrc);
    // ...

    // then push the product into the list
    products.push( $prodRow );
}


// Finally, put the products wherever they go in the DOM
var $container = $('Selector of ul or ol element into which to put the product <li>s');
$container.append( products );

Obviously, define your array of objects as appropriate.

1 Comment

thanks! i will try this and tell the result later :)

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.