Is it possible to create a grid by arranging each statement with a specific format?

After fetching and parsing some data, I have utilized the 'each' function to display it:

$.ajax({
    url      : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent('http://news.google.com/news?pz=1&cf=all&ned=uk&hl=en&output=rss'),
    dataType : 'json',
    success  : function (data) {
        if (data.responseData.feed && data.responseData.feed.entries) {
            $.each(data.responseData.feed.entries, function (i, e) {
                $('.row').append("<div class=\"threecol\"">title: " + e.title + "<br></div>");
            });
        } 
    }
});

In order for the grid to work properly, it must adhere to this structure:

<div class="container">
    <div class="row">
        <div class="threecol">
        </div>
        <div class="threecol">
        </div>
        <div class="threecol">
        </div>
        <div class="threecol last">
        </div>
    </div>
</div>

My query revolves around how to organize the each loop so that every set of 4 items creates a new row, with the 4th item having the class "threecol last" instead of just "threecol"?

Answer №1

Prior to your $.each loop, implement a counter and incorporate simple conditions to correctly place the rows while assigning appropriate classes:

if (c % 4 == 0) { 
  $('<div />').addClass('row').appendTo('.container'); 
}

$('<div />').addClass('threecol')
            .text('title: ' + e.title)
            .appendTo('.row:last');

if (c % 4 == 3) {
  $('.threecol:last').addClass('last');
}

c++;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When an input field is added to an HTML form, all elements positioned below it automatically inherit its attributes

I have a unique combination of HTML and CSS code that creates an impressive login form: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta charset="utf-8"> <title>Prepare to be amazed...</title> <l ...

Why does the Hamburger Menu shift my website's logo when it opens?

I'm in the process of implementing a hamburger menu on my website. My attempts have involved adjusting the positioning of both the #logo and .topnav elements. Code Snippet source function myFunction() { var x = document.getElementById("myTopn ...

What is the best way to load a partial in Rails asynchronously with AJAX?

I am currently using the following code to load a partial when I reach the bottom of a div containing a table: $(document).ready(function () { $("#pastGigs").scroll(function () { if (isScrollBottom()) { $('#pastGig ...

When the JQuery event-listener for .show('drop', 500) has completed execution

Currently on the lookout for an event listener that can be used to verify when the animation using .show('drop', 500) has completed. The desired usage would be as follows: if($('#id').show('drop', 500) == complete){ // perfo ...

Step-by-step guide on aligning a div of unknown height in the center of a div with a set

Struggling to center product images vertically within a fixed height box? Here's the code I've been working with: <div class="main-pic"> <ul> <li><img src="images/extra/laptop.jpg"></li> <li><img ...

How can you use the :not selector in CSS to target specific elements within a group?

Consider the following set of inputs: <div id="group"> <input id="uno" class="red"></input><br/> <input id="dos" class="red"></input><br/> <input id="tres" class="blue"></input><br/ ...

Ensure all asynchronous Ajax requests have completed before considering the page fully loaded

Many websites, such as YouTube, load the majority of their content after the DOM is ready using JavaScript. How can I ensure that all of these requests have been completed before injecting my code? window.onload = function() {}; The above code snippet do ...

Getting user input with JQuery and dynamically updating CSS properties

<img src="purplemoon.png" alt="Purple moon" id="moon-photo" /> <table> <tr> <th colspan="4">Control panel</th> </tr> <tr> <td> <!-- attempting to retrieve value from the input field ...

Ways to activate javascript following a PHP submission?

It's a bit tricky to explain. function m(val){ var element=document.getElementById('othermethod'); if(val=='others') element.style.display='block'; else element.style.display=&apo ...

Is it possible to switch from a dropdown menu to radio buttons?

I am looking to change the dropdown menu in my search section into radio buttons. Currently, when I select something from the dropdown menu, the search fields are altered. Here is the code for the dropdown menu: <select id="qs_category" name="qs_catego ...

Struggling to link variables and functions to an angularJS controller

When using the $scope object to bind functions and variables, and making changes accordingly in HTML, the code works fine. But when using a different object, it doesn't work as expected. Here is an example of a working code snippet: ...

iOS fixed positioning and scrolling

One of the current challenges we are facing involves implementing an action button similar to the one found in GMail for adding new content. This button needs to remain fixed during scroll. Here is the HTML code: <div class="addButton actionButton" on ...

Verify if a specific key is present in associative arrays

Can you please explain the correct way to check for the existence of a key in associative arrays? For instance: var mydata = { key1: '', key2: { subkey1: { subkey1_1: { value1: '' ...

Effortlessly sending multiple values from text fields using jQuery

i am using jQuery to fetch all values from text fields with the same field name. $('input[name^="StudentName"]').each(function() { StudentName += $(this).val(); alert(StudentName); }); when I alert, all the values are displayed as one s ...

Is there a way for me to move the dot icon along the dotline?

Here is the code snippet: jsFiddle I have successfully implemented a click event to change the style of selected dots in dotline. Now, my query is : How can I enable dragging functionality for the dot icon within dotline? Your assistance on this matte ...

Customize WPBakery Accordion Background Color

I'm currently using WPBakery's Accordion section, version 5.4.7, to showcase a Gravity Form. I am attempting to modify the background color of the active accordion section to black. After exploring various forum examples, I have attempted to add ...

Fetching large images with "fill" layout in NextJS

I am currently using Cloudinary to serve correctly sized images, however, it appears that NextJS image is always fetching with a width of 3840, instead of the fixed value. <Image src={loaderUrl(logoImage)} alt="" role="presen ...

Encountering a Rails 500 server error when making an Ajax request to a form_for partial

As part of my blog creation process, I am working on implementing a dashboard feature where users can easily select "New" from the side menu to bring up a form for creating a new article. To achieve this functionality, I have been experimenting with usin ...

php retrieve and show data from MySQL database based on selection from dropdown menu

I've encountered an issue where I can retrieve data from a database and display it in a drop-down menu, but I'm not sure how to use the same drop-down list as input within a form. My code retrieves project names from the database and displays the ...

Launch a YouTube video within a sleek and stylish Bootstrap modal popup

I am currently extracting video data from my SQL table. The fields in the table are as follows: - sidebar_video_id (auto increment) - sidebar_video_nev - sidebar_video_link (full URL) - sidebar_video_v_id (video ID at the end of the URL) What I'm tr ...