Arrange a trio of cards in a row with dynamic alignment

I'm having trouble aligning the cards next to each other. I've written the code below but for some reason, they're not displaying next to each other.

What I'm trying to do is call a REST API and get back a list of data. I want to loop through the results and display them in card format. Can someone help me with this? Thank you.

client.js

 $(document).ready(function() {   
        $.ajax({
           url: "REST API CALL",
           success: function(data){        
            data.forEach(function(a) { 
                var html = `      
                <div class="row">
                   <div class="">
                    <div class="card" style="width: 20rem; text-align: center; display: inline-block;">
                    <img class="imagee" src="im.png" alt="image" height="200" width="200" style="border-radius: .60rem;">
                     <div class="card-body">
                     ${a[0]}|${a[4]}<br>
                     ${a[7]}<br>
                     <i class="fa fa-car"></i>${a[9]}
                  <p class="card-text"></p>
           </div>
    </div>
      </div>
    </div>`

                $('#msgs').append(`<div>${html}</div>`);
              });
             }
        }).then(function(data) {      
        });
        });

Answer №1

Revise your code below, and ensure that the <div class="row"> is placed outside of this function and encloses the html item with an id="msgs", as the div class row should encompass all card elements.

$(document).ready(function() {
  $.ajax({
    url: "REST API CALL",
    success: function(data) {
      data.forEach(function(a) {
        var html = `      
                <div class="card col-4" style="text-align:center;">
                    <img class="imagee" src="im.png" alt="image" height="200" width="200" style="border-radius:.60rem;">
                    <div class="card-body">
                        ${a[0]}|${a[4]}<br>
                        ${a[7]}<br>
                        <i class="fa fa-car"></i>${a[9]}
                        <p class="card-text"></p>
                    </div>
                </div>`;
        $("#msgs").append(`<div>${html}</div>`);
      });
    }
  }).then(function(data) {});
});

Answer №2

Big thanks to everyone who helped point me in the right direction. Here's the solution I discovered for arranging the cards in a horizontal line using a for loop with col-lg-3.

     var html = ` 
            <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>

            <div class="col-lg-3" ng-app=""><br>
            <div  class="card" style="width:20em;text-align:center;"><br>
              <img ng-if="'${a[2]}'=='null'" src="image" height="200" width="200" style="border-radius:.60rem;">

               <div class="card-body">
                &nbsp;${a[0]} &nbsp;| &nbsp;${a[4]}<br>

                 &nbsp;${a[7]}<br></td>
                 <tr><td>
                 &nbsp;<i class="fa fa-bed"></i>&nbsp; ${a[1]} &nbsp;

</div>
  </div><br>
</div>`

$('#msgs').append(`<div>${html}</div>`);

          }); 

Answer №3

One way to customize the appearance of your website is by assigning the class col-md-4 to a div located inside another div with the class row

$(document).ready(function() {   
    $.ajax({
       url: "API CALL HERE",
       success: function(data){        
        data.forEach(function(a) { 
            var html = `      
            <div class="row">
               <div class="">
                <div class="card colo-md-4" style="width: 20rem; text-align:center;display:inline-block;">
                <img class="imagee" src="im.png" alt="image" height="200" width="200" style="border-radius:.60rem;">
                 <div class="card-body">
                 ${a[0]}|${a[4]}<br>
                 ${a[7]}<br>
                 <i class="fa fa-car"></i>${a[9]}
              <p class="card-text"></p>
       </div>
</div>
  </div>
</div>`

            $('#msgs').append(`<div>${html}</div>`);
          });
         }
    }).then(function(data) {      
    });
    });

Answer №4

The forEach loop's rendering logic must be adjusted. Make sure to repeat the columns and then wrap them all in a row element.

     data.forEach(function(item) { 
        html += `      
           <div class="col-4">
            <div class="card" style="width:20em;text-align:center;display:inline-block;">
                <img class="imagee" src="//placehold.it/200" alt="image" height="200" width="200" style="border-radius:.60rem;">
                 <div class="card-body">
                 `+item[0]+`<br>
                 <i class="fa fa-car"></i>
                  <p class="card-text"></p>
                </div>
              </div>
        </div>`;
     });
     $('#msgs').append(`<div class="row">`+html+`</div>`);

Check out the demo here:

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

The iFrame's getFocus function does not activate

I have an iframe set up like this: <iframe name="richTextField" id="richTextField" style="border:#000 1px solid; width:700px; height:300px;"></iframe> I also have some JavaScript code that is supposed to trigger when the iframe gains or loses ...

Creating dynamic visual representations on a website by generating real-time graphs

Every minute, new data is added to my csv file. I would like to display this data as graphs on a webpage. The file contains 12 parameters and I am interested in creating charts that show the relationship between each parameter and time. ...

Contrasting results when logging an element in Chrome versus IE

Running the script below in Internet Explorer gives the expected output for console.log(target_el): <div class="hidden"></div> However, when run in Chrome, the output changes to: <div class="visible"></div> To add a humorous twi ...

Managing asynchronous model data in EmberJS version 1.0.0

Working on a project at my job using EmberJS v1.0.0-pre.4, which is quite old now. Since it's for work and upgrading might be problematic (not sure if our company will want to), I need to come up with a workaround. I simply need to load data for my m ...

What steps should I take to ensure a local HTML page retains the current section that is hidden open whenever it is reloaded?

One of the challenges I have encountered with my local HTML note-taking file is that, despite dividing it into hidden sections accessible by clicking on buttons at the top of the page, reloading the content resets it back to its default state. This means t ...

Angular 6+ has a revolutionary theme service that seamlessly impacts all components as well as modals using the <ng-template let-modal>

As per the post How to toggle a class using a button from the header component in Angular 6+ I implemented a theme service which successfully toggles the 'dark-mode' class in the app.component.html. However, I encountered an issue where all my m ...

The PHP-based registration process is malfunctioning

Trying to simply register, but I'm encountering an issue where the file is named Login.html/.php instead of just Login.html. Below is my HTML form in Login.html: <!DOCTYPE html> <html> <head> <title>Register/ Login</title&g ...

HTML input form - issue with combining multiple patterns

Below is a section of my HTML code: <input type="text" #version tabindex="1" class="form-control" id="version" name="version" placeholder="" [(ngModel)]="application.version" required minlength="1" pattern="\d{1 ...

Is the same-origin policy being breached?

Hello there, I've been encountering an issue with my jQuery AJAX Call to retrieve data from a self-hosted webservice on the same domain. The response always returns as 0, indicating a cross-domain problem, even though it shouldn't be occurring. ...

JavaScript News Scroller for Horizontal Display

After searching through numerous websites, I have yet to find the ideal horizontal news scroller for my website. My specific requirements include: Must provide a smooth scrolling experience Scroll from right to left covering 100% width of the browser ...

Creating a Web Form in Outlook Using the POST Method

Seeking help in creating an HTML email featuring two interactive buttons - Approve and Reject. I have successfully generated the HTML email and sent it to Outlook. Snapshot: https://i.sstatic.net/NrGFM.png The HTML code within the email: <!DOCTYPE ...

Steps for embedding JavaScript code within HTML tags using a JavaScript file

Working on a React web app, I am solely using js and css files without any html. In one of my js files, there is a mix of html and js code like this: class Teams extends Component { state = { teams: [] } componentDidMount() { ...

The styled component is not reflecting the specified theme

I have a suspicion that the CSS transition from my Theme is not being applied to a styled component wrapped in another function, but I can't pinpoint the exact reason. I obtained the Basic MUI Dashboard theme from this source and here. Initially, inte ...

Troubleshooting responsive navigation bar in VueJs: Why are elements not appearing when ToggleButton is clicked?

I'm developing a VueJs single page application and I'm struggling to implement a responsive NavBar. No matter what I try, it just doesn't seem to work as expected. I've experimented with several solutions, and the closest I have come t ...

Highlighting the selected tab with an active class

Currently in the process of learning angularJs, I am still new to it and attempting to dynamically add an active class to the recently clicked tab. Within my interface, there are four tabs: insert, view, update & delete. My goal is to apply the active ...

Customize the overlay color using the theme's background color

I want to create an overlay on my webpage that covers all the content. However, I'm facing a challenge because my website allows users to customize the theme colors, and the overlay div does not adopt these color changes automatically. To rectify this ...

PHP server-side detects empty AJAX post requests

I am encountering an issue while making an AJAX request to a PHP controller using jQuery ajax. The problem arises when attempting to access the posted data in PHP, as the $_POST variable is empty. Below is the function causing the trouble: function GetSer ...

Detecting when the page is done loading in CasperJS with the help of $.ajaxStop()

During my CasperJS tests, I've relied on waitForSelector() to check if a page has finished loading, including all asynchronous AJAX requests. However, I'm interested in finding a more standard approach for waiting for page load. Is it possible to ...

Issue with toggleClass functionality (potential coding glitch)

I am attempting to display my div once the button is clicked. Below is the code snippet: <div class="post-menu"> <button class="button"> <i class="uil uil-ellipsis-h"></i> </button> ...

Leveraging the Google Feed API using jQuery's AJAX functionality

Currently, I am attempting to utilize Google's Feed API in order to load an RSS feed that returns a JSON string. (For more information, please refer to: https://developers.google.com/feed/). Despite this, my approach involves using jQuery's AJ ...