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

What could be causing my component to fail to load properly with Vite?

I have been working on a React project using Vite. Following a tutorial I discovered in an article at https://www.digitalocean.com/community/tutorials/how-to-set-up-a-react-project-with-vite, I encountered an issue. Despite following the tutorial step by ...

Vue.js is able to update various models based on selection from a form dropdown

I have a dynamic select list in my app built with vue.js. I am trying to update a "details" box on the page with information fetched through an ajax request. You can view the code here: https://jsfiddle.net/pznej8dz/1/ I am puzzled as to why the sf_detail ...

Adjusting the height of flex items to 100%

I'm currently diving into the world of flexbox by exploring various tutorials. Here's an example I created without utilizing Flex. Instead, I relied on float:left. Check out the example: https://jsfiddle.net/arhj8wxg/4/ I attempted to convert t ...

Troubleshooting problems with deploying an Angular app on Heroku

When I attempt to deploy my Angular app on Heroku, I am running into a 'Not found' error and getting additional information in the console ("Failed to load resource: the server responded with a status of 404"). Below is the entire Heroku build l ...

Creating an alert pop-up that displays text based on the prompt input type: A step-by-step guide

I'm a new to Javascript and I'm trying out some basic scripts. My objective is to display a prompt message (1.) asking for a name, then show an alert (2.) based on the input from the prompt. If the input is a valid name (a string), then the alert ...

Jade iterates over each object element, assigning its children to their respective parent elements

I have a JavaScript Object named "boards". [{"id":1,"parent_board":0,"title":"Lorem 1","description":"ec40db959345153a9912"}, {"id":2,"parent_board":0,"title":"Lorem 2","description":"bb698136a211ebb1dfedb"}, {"id":3,"parent_board":1,"title":"Lorem 1-1"," ...

Issues with jQuery autocomplete functionality on certain elements are not uncommon

I've been experimenting with creating a user script for Opera using Greasemonkey to implement autocomplete functionality on input elements within web pages. However, I've encountered some issues with the script not working as expected. Initially ...

Loading bar for AngularJS material framework

Is there a way to integrate https://material.angularjs.org/latest/demo/progressLinear into my website so that it shows progress when a new view is loading? I'm trying to figure out how to obtain the value of the current page being loaded. Any suggest ...

Practical Tip for jQuery - Storing DOM Element in a Variable for Consistent Usage Across Your Codebase

As I was searching for some Jquery best practices, I came across this gem. Source: // Storing the live DOM element in a variable var elem = $("#elem"); // Setting an element's title attribute using its current text elem.attr("title", elem.te ...

Revise the if statement by using variables

Here is a function that already exists in a theme: function WShare( selector ) { var $this = $( selector ), $parent = $this.parent(); var opt = { url: window.location, text: document.title, }; if ( window.selectedText ) { ...

No validation errors are currently being displayed. I am attempting to retrieve the error message when validation fails

My form validation error has suddenly stopped working. It was functioning properly yesterday, so I must have made a mistake somewhere, but I can't seem to pinpoint it. Now, when I attempt to sign up without entering any information, it just redirects ...

What is the process of adding information to a JSON file?

I'm looking to store my data in an external JSON file and have it update the list when the page is reloaded. Can anyone assist with this? Below is my code: $scope.addUser = function() { var user = { id: null, login: '', ...

Retrieving details of a row in Codeigniter using a link with a foreach loop

After nearly a month of trying, I am still unable to figure out how to extract the "details" for each row from my table in the view. The table is populated using a foreach loop and I want to display these details on another page when clicking the link labe ...

How to conceal hyperlink underline with css

This is some HTML Code I'm working with <a href="test.html"> <div class=" menubox mcolor1"> <h3>go to test page</h3> </div> </a> Here's the corresponding CSS: .menubox { height: 150px; width: 100%; ...

Utilizing TypeScript namespaced classes as external modules in Node.js: A step-by-step guide

My current dilemma involves using namespaced TypeScript classes as external modules in Node.js. Many suggest that it simply can't be done and advise against using namespaces altogether. However, our extensive codebase is structured using namespaces, ...

Text that only occupies a portion of the screen width

My unordered list (ul) contains three list items (li). The first li element displays the text "opptakskrav" and the last li element displays "ja". Can anyone explain why the text in my second li element does not use the full width and starts a new line hal ...

Ensure NodeJS/JSDom waits for complete rendering before performing scraping operations

I am currently facing an issue with scraping data from a website that requires login credentials. When I try to do this using JSDom/NodeJS, the results are inconsistent compared to using a web browser like Firefox. Specifically, I am unable to locate the l ...

Trigger Element Upon Click

Forgive me in advance for the lack of quality in this question, but I'll proceed anyway: All I want is for an element to slide open when clicked with a mouse! That's all! More specifically, I am looking for a single menu item that, upon clickin ...

The JSON.stringify method in TypeScript/JavaScript does not align with the json-String parameter or request body in a Java controller

Currently, I am utilizing jdk 1.8 and have a rest endpoint in my Java controller: @PostMapping("/filters") public ResponseEntity<StatsDTO> listWithFilter( @RequestBody(required = false) String filter ) { try { ............... } } A test sn ...

Unusual outcomes observed while implementing Dependency Injection in multiple AngularJS applications

Due to the original development setup of my company's website using ASP.NET with ascx pages and code behind, I had to resort to manually bootstrapping Angular in order to have two Angular applications on the same page. One application controls the mai ...