One way to generate div elements based on the number in an input field when a button is clicked, but ensuring it only happens once

What I am attempting to achieve is:

  1. Retrieve data from a JSON file upon button click.
  2. Display the data in separate boxes, each one different for every element of the array. For instance, if the JSON provides 3 rows of data, there should be 3 distinct boxes displayed.
  3. Arrange the boxes in a grid of 2X2.
  4. Exactly how it's illustrated in this wireframe image Wireframe for foodcourts

I have easily accomplished Step 1. I'm able to generate boxes on button click but not in the desired layout shown in the image. Additionally, each time the button is clicked, the new content is added to the div. Any assistance would be greatly appreciated. Below is my code:

<!doctype html>
<html ng-app="myApp">
  <head>
    <meta charset="utf-8">
    <title>Untitled Document
    </title>
  </head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
  </script>
  <style>
    .shape{
      text-align:center;
      background-image:url(dark-triangles.png);
      width:200px;
      height:60px;
      line-height:60px;
      color:white;
      margin:10px;
      position:relative;
      transition-property: background;
      transition-duration: -10s;
      transition-timing-function: linear;
    }
    /* Remaining CSS styles here...*/

          </div>
        </body>
      </html>

Answer №1

There were several issues that needed to be addressed:

  • The style tag should always be placed in the head section
  • The button elements must have a closing tag like this: <button></button>
  • The input tag should also be closed with a forward slash like so: <input />
  • The script tag is best placed in the head or at the bottom of the body

I have made some adjustments to float the div elements, allowing you to achieve your desired outcome. Feel free to modify the code as needed:

    angular.module('myApp', [])
      .controller('myController', function($scope, $http) {
        $scope.fcclicked = function() {
          alert("hello");
          $http.get('http:/18.220.71.157:8080/foodcourt/foodcourt/list')
            .success(function(response) {
              $scope.names = response.foodCourts;
              $scope.len = response.foodCourts.length;
            });
        };
      })
.shape {
    text-align: center;
    background-image: url(dark-triangles.png);
    width: 200px;
    height: 60px;
    line-height: 60px;
    color: white;
    margin: 10px;
    position: relative;
    transition-property: background;
    transition-duration: -10s;
    transition-timing-function: linear;
  }
  
  .shape:before {
    content: "";
    width: 0px;
    height: 0px;
    border-top: 60px solid rgba(107, 255, 55, 105);
    border-left: 60px solid transparent;
    position: absolute;
    right: 0%;
    top: 0px;
  }
  
  .shape:hover {
    background: #ff7b29;
    cursor: pointer;
  }
  
  body {
    font-family: "Lato", sans-serif;
    box-sizing: border-box;
  }
  
  .sidenav {
    height: 100%;
    width: 250px;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-image: url("doodles.png");
    background-color: #cccccc;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
  
  .div1 {
    box-sizing: border-box;
    width: 400px;
    height: auto;
    background: none;
    margin: auto;
    border: 2px solid black;
    margin-top: 70px;
    overflow: hidden;
  }
  
  .divcontent {
    width: auto;
    height: auto;
    background: none;
    margin: auto;
    border: 2px solid black;
    margin-top: 70px;
  }
  
  .square {
    box-sizing: border-box;
    width: 48%;
    height: 100px;
    background: red;
    display: inline-block;
    margin: 1%;
    float: left;
    padding: 10px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<!doctype html>
<html ng-app="myApp">

<head>
  <meta charset="utf-8">
  <title>Untitled Document
  </title>
</head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>

<body ng-controller="myController" background="cloudy-day.png" style="color:black;">
  <div id="mySidenav" class="sidenav">
    <input value={{len}} />
    <button class="shape" id="orders">Orders</button>
    <button class="shape" id="users">Users</button>
    <button class="shape" ng-click="fcclicked()">Food Courts</button>
    <button class="shape">Delivery Locations</button>
  </div>
  <div class="div1" id="d1">
    <p align="center">Click one of the buttons to load content.....
    </p>
    <div class="square" ng-repeat="x in names">
      {{ x.id}} {{ x.name }} {{ x.address }}
    </div>
  </div>
</body>

</html>

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

Is it achievable to have a background image cover size with a responsive rollover effect?

I’m currently facing a unique challenge where I want to have an image as the background of my website, with the size set to cover the entire screen. On this background image, there are elements like buildings that I want to interact with when hovered ove ...

Image carousel with interactive buttons

I've taken over management of my company's website, which was initially created by someone else at a cost of $24,000. We recently made some edits to the slideshow feature on the site, adding buttons that allow users to navigate to corresponding p ...

The integration of Firebase Google Auth seems to encounter issues when used on the Vercel

My experience with Vercel deployment has been quite interesting. While I find that Google authentication works seamlessly on localhost, it seems to encounter an issue when deployed on Vercel. Specifically, after logging out, the currentUser variable always ...

Can you please elaborate on the concept of type coercion in JavaScript?

I've come across information stating that when comparing an object with a number, type-coercion occurs. ToPrimitive is called on the object which then invokes valueOf and, if necessary, toString. However, I'm struggling to understand how this pro ...

Issue: Incompatibility in metadata versions detected for module .../ngx-masonry/ngx-masonry.d.ts. Level 4 version identified, whereas level 3 version

When using ngx-masonry, I encountered the following error message- ERROR in Error: Metadata version mismatch for module .../ngx-masonry/ngx-masonry.d.ts, found version 4, expected 3 Specifications: Angular 4 ngx-masonry 1.1.4 ...

Creating a dynamic webpage where multiple images are displayed on a single canvas using

I'm currently working on a chess project where I'm using a canvas to create the game board and draw 32 chess pieces on it. To update the screen when a piece moves, I've implemented requestAnimFrame. However, I'm facing an issue where th ...

Invoking getJSON() repetitively within a loop inside another getJSON() call, within a new row specifically

Although I am not well-versed in JS, I have a feeling that I might be incorrect in the way I am handling this situation. The general idea here is to query the database for a list of items; then within the initial loop, create a row in the table by using th ...

What is the best way to ensure the network is idle after clicking on an element in puppeteer?

Is there a way to wait for network idle after clicking on an element in puppeteer? const browser = await puppeteer.launch({headless: false}); await page.goto(url, {waitUntil: 'networkidle'}); await page.click('.to_cart'); //Clicking o ...

Implementing JavaScript to showcase a list extracted from an API dataset

I'm currently undertaking a project where I am integrating an API from a specific website onto my own webpage. { "data": [{ "truckplanNo":"TCTTV___0D010013", "truckplanType":"COLLECTION", " ...

What is the best way to present a unique page overlay specifically for iPhone users?

When browsing WebMD on a computer, you'll see one page. However, if you access it from an iPhone, not only will you be directed to their mobile page (which is easy enough), but they also display a special overlay with a "click to close" button prompti ...

What could be causing req.body to consistently come back as an empty object?

I am struggling with req.body always returning an empty object regardless of what I try. I have experimented with: var jsonParser = bodyParser.json(); and then including jsonParser in the function -> app.post('/api/get-last-project',jsonParser ...

What do you want to know about Angular JS $http request?

My goal is to send a request using $http with angular js in order to retrieve a json object from google maps. $http.get('http://maps.googleapis.com/maps/api/geocode/json?address=' + data[ 'street' ] + ',' + data[ 'city&a ...

Check if the user is null using React's useEffect hook and then perform a

I am currently working on implementing a protected route in Next JS. I have included a useEffect to redirect to the sign-in page if there is no user detected. const analytics = () => { const { userLoaded, user, signOut, session, userDetails, subscri ...

Is it possible to automatically redirect to a different URL if the server is running slow when clicking?

Is it possible to utilize Javascript, AJAX, or another client-side technology to automatically redirect the user to a different URL if the initial URL is slow to respond when clicked on? For example, if a link redirects to URL1 and there is no response fr ...

Trouble with custom font updates in Hugo using Blogdown

Recently, I've been immersing myself in Blogdown to create my own personal data blog: Using the academic theme as a base, I successfully tweaked the color scheme without any hiccups. However, despite making adjustments in my params.yaml file, none o ...

Dynamic urls from previous getJSON calls are utilized in nested getJSON calls

I am seeking assistance in finding a more dependable method for accomplishing this task. We are working with 3 fixed URLs that cannot be modified. The process begins with a getJSON call to url1 to retrieve all the sections. Within the callback function ...

What could be causing the overlap between the button and the div element?

My main wrapper div contains a content div and a button. However, I'm facing an issue where the button is overlapping with the content div instead of appearing below it. The content div is styled with the following CSS: #groupMembers { position: ...

Tips for creating multiple files using nodejs and express

I am currently working on developing a personalized code editor that consists of 3 textareas: html, css, and javascript. The objective is to save the data from each textarea into individual files. With the help of express and nodejs, I have successfully m ...

Understanding the specific purpose and functionality of the "next()" parameter in express.js

As a newcomer to express.js, I have been diving into the documentation to understand its nuances. One particular aspect that caught my attention is the next parameter in express middleware, which supposedly triggers the following middleware function for th ...

Attach the JSON data to the dropdown menu using jQuery

On my asp.net website, I have generated a JSon string using the JavaScriptSerializer class in JavaScript. Then, I displayed the Json string within the HTML markup by utilizing the RegisterStartupScript method and here is how it appears: This is the C#.net ...