Gathering information from an HTML form and displaying it through JavaScript by utilizing Bootstrap's card component

I've been working on integrating form data from my index.html page into a card component using Bootstrap. I've successfully collected the data in the backend, but now I'm struggling to display it on the frontend as a card component. Any help would be greatly appreciated!

Here's a snippet of my index.html code :

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="styles.css">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99fbf6f6edeaedebf8e9d9acb7a9b7a8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"> 
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c0f031e092c5e4255425e">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5a7aaaab1b6b1b7a4b585f0ebf5ebf4">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script></head>
<body>  
  <header>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <div class="container-fluid ">
        <a class="navbar-brand " href="index.html">EBlogs</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>
    </nav>
  </header>  
  <main>
    <div class="card" style="margin-left: 500px; margin-top: 100px; width: 40rem;">
        <img src="./images/mountains.jpg" class="card-img-top" alt="mountains">
        <div class="card-body">
          <h5 class="card-title">Mountains</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <button type="button" class="btn btn-primary" onclick="UpdateImage()">Update Blog</button>
          <button type="button" class="btn btn-danger" onclick="addImage()">Delete Blog</button>
        </div>
      </div>
      <form id="addBlog" method="POST">
        <div class="container contaier-lg">
        <div class="mb-3">
          <label for="Title" class="form-label">Title for a Blog :</label>
          <input type="text" class="form-control" id="title" aria-describedby="emailHelp">
        </div>
        <div class="mb-3">
          <label for="Content" class="form-label">Content :</label>
          <input type="text" class="form-control" id="content">
        </div>
        <div class="mb-3">
            <label for="User" class="form-label">Created By :</label>
            <input type="text" class="form-control" id="username">
          </div>
        <button type="button" class="btn btn-primary" onclick="addImage()">Add Blog</button>
      </form>
    </div>
  </main>
<script src="backend.js"></script>
</body>
</html>

This is part of my backend.js file :

var card = []
function addImage(){

var title = document.getElementById("title").value
var content = document.getElementById("content").value 
var user = document.getElementById("username").value 
card = {title,content,user}

function adder(card){
    for(let i=0;i<card.length;i++){
        card.push(card[i])
    }
    return card
}
console.log(adder(card))
}

I have tried using append method with inner.HTML methods but haven't had any success so far.

When I add the contents, the view should resemble something like this : https://i.stack.imgur.com/aJfmC.png

Answer №1

let blog = []
function createBlog(){
let title = document.getElementById("title").value
let content = document.getElementById("content").value 
let user = document.getElementById("username").value 
blog = {title,content,user}

let blogTemplate = '    <div class="card" style="margin-left: 500px; margin-top: 100px; width: 40rem;">                                            '+
    '    <img src="./images/mountains.jpg" class="card-img-top" alt="mountains">'+
    '    <div class="card-body">'+
    '      <h5 class="card-title">'+ title +'</h5>'+
    '      <p class="card-text">'+ content +'</p>'+
    '      <button type="button" class="btn btn-primary" onclick="UpdateBlog()">Update Blog</button>'+
    '      <button type="button" class="btn btn-danger" onclick="createBlog()">Delete Blog</button>'+
    '    </div>'+
    '  </div>'
    '</div>';

document.getElementById("addBlog").insertAdjacentHTML("beforebegin", blogTemplate);

function blogger(blog){
    for(let i=0;i<blog.length;i++){
        blog.push(blog[i])
    }
    return blog
}
console.log(blogger(blog))
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="styles.css">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"> 
  <script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-Atwg2Pkwv9vp0ygtn1JAojH0nYbwNJLPhwyoVbhoPwBhjQPR5VtM2+xf0Uwh9KtT" crossorigin="anonymous"></script></head>
<body>  
  <header>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
      <div class="container-fluid ">
        <a class="navbar-brand " href="index.html">EBlogs</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>
    </nav>
  </header>  
  <main>
    <div class="card" style="margin-left: 500px; margin-top: 100px; width: 40rem;">
        <img src="./images/mountains.jpg" class="card-img-top" alt="mountains">
        <div class="card-body">
          <h5 class="card-title">Mountains</h5>
          <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
          <button type="button" class="btn btn-primary" onclick="UpdateImage()">Update Blog</button>
          <button type="button" class="btn btn-danger" onclick="createBlog()">Delete Blog</button>
        </div>
      </div>
      <form id="addBlog" method="POST">
        <div class="container contaier-lg">
        <div class="mb-3">
          <label for="Title" class="form-label">Title for a Blog :</label>
          <input type="text" class="form-control" id="title" aria-describedby="emailHelp">
        </div>
        <div class="mb-3">
          <label for="Content" class="form-label">Content :</label>
          <input type="text" class="form-control" id="content">
        </div>
        <div class="mb-3">
            <label for="User" class="form-label">Created By :</label>
            <input type="text" class="form-control" id="username">
          </div>
        <button type="button" class="btn btn-primary" onclick="createBlog()">Add Blog</button>
      </form>
    </div>
  </main>
<script src="backend.js"></script>
</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

``JsViews and AngularJS: A Comparison"

I'm exploring the possibility of creating a single page application and came across jsViews/jsRender which seems very promising as it approaches Beta. As someone new to SPA development, I'm interested in understanding how jsViews stacks up agains ...

Display information in a paginated format using components

As a newcomer to React, I may use the wrong terms so please bear with me. I am attempting to implement pagination for an array of components. To achieve this, I have divided the array into pages based on the desired number of items per page and stored eac ...

Is it possible to incorporate a PHP if-else statement in the action attribute of an HTML form?

I'm currently developing a form to allow users to create a login username and password. Once the account creation is successful, I want the user to be directed to the actual LOGIN form. To manage this process, I've implemented various checks usi ...

Error 403 occurs after submitting the form

Recently, I encountered a 403 error when attempting to submit the form on this page. Interestingly, when I directly accessed the new page by typing the URL into my browser, it loaded without any issues. The PHP code in the initial page looks like this: & ...

When running `aws-cdk yarn synth -o /tmp/artifacts`, an error is thrown stating "ENOENT: no such file or directory, open '/tmp/artifacts/manifest.json'"

Starting a new aws-cdk project with the structure outlined below src └── cdk ├── config ├── index.ts ├── pipeline.ts └── stacks node_modules cdk.json package.json The package.json file looks like this: " ...

Generating an array from multiple worksheets and implementing a For Each loop

Currently, I am working on creating an array from multiple worksheets and utilizing a For Each loop to access them. This method has been quite successful for most tasks, but I have encountered an issue with sorting the tables on the worksheets. Could some ...

Is there a way to display a specific dropdown menu depending on the checkbox that is selected?

I have a bunch of checkbox items, including one labeled nocalls, as well as a couple of dropdownlist boxes. Here are the dropdown boxes: <tr> <td align="right"><FONT class="Arial10"><B>Profile<font color="#ff0000">*</ ...

What is the method to access nested JSON data returned from the Angular JS $http service?

Here is the main JSON file I am using { "chartType" : ["column", "column", "pie"], "chartTitle": ["Number of teams", "Number of consumable items","Number of employees in charge"], "yAxisTitle": ["Teams", "Consumables", "Employees"], "seriesName": ...

can you explain the concept of a backing instance in react?

Although the concept of a "backing instance" is frequently mentioned in React documentation, I found it difficult to grasp its meaning. According to the React docs: In order to interact with the browser, you need a reference to a DOM node. By attaching ...

What is the best way to create an event listener that can identify when a boolean variable switches to true?

Let's say we have a variable var menu_ready = false;. We also have an ajax function that will change menu_ready to true once the ajax operations are completed: // code snippet to set up event listener $(...).load(..., function() { ... menu_r ...

Angular static dropdown option

<input [formControl]="twitterHandle" id="twitterHandle" placeholder="twitterHandle"> Fetching the input value from the Twitter handle input field using the following code: twitterHandle = new FormControl(); this.twitterHandle.value; Similarly, if ...

Encountering a bug: "Undefined property" issue when working with Web Sockets in React Native

I'm a beginner in React Native and Java Script development, and I'm facing an issue while trying to retrieve a JSON object and display it on the client side. I keep getting a "cannot read property of undefined" error when using websockets instead ...

Implement modals using a for loop in Vue

I am facing an issue while trying to create modals for cards within loops. I attempted using v-bind:id="masa._id" and v-bind:data-target="'#'+masa._id", but the modal is not working. Can someone guide me on how to implement modals in loops? Belo ...

In JavaScript, you can verify if a specific <input> element is an input-field

With the introduction of HTML5, a variety of new input types have been added: <input /> Is there a way to detect whether an input field is a text field (such as date, time, email, text, etc.) without explicitly specifying each type? I would like t ...

Next.js is unable to serialize the response from getServerSideProps functions into JSON

I am currently in the process of developing a Next.js application that consists of multiple pages utilizing dynamic routing. Each page within the application involves making various axios calls to the backend using useEffect hooks. In order to enhance the ...

Save an automatically generated number into a variable and use it to reference an image file for display. This process can be accomplished using JavaScript

I'm having trouble getting my images to display randomly on a page. The images are named 0 - 9.png and I am using a pre-made function for random number generation. However, when I try to call on this function later down the page, nothing appears. It ...

Ways to forward a parameter to a different web address?

Is there a way to properly redirect a parameter such as http://example.com/?link=https://google.com to its destination at ? ...

Facing difficulties with the XPATH due to an inability to access specific parts of the HTML code

Currently, I am facing an issue where I need to click on a link using Selenium and Java. When searching for the link using XPath, I am encountering a problem where only white spaces are displayed instead of most of the webpage content. The highlighted link ...

Issue with Moment.js: inability to append hours and minutes to a designated time

I have a starting time and I need to add an ending time to it. For example: start=19:09 end=00:51 // 0 hours and 51 minutes I want to add the 51 minutes to the 19:09 to make it 20:00. I've attempted several different methods as shown below, but none ...

If I do not utilize v-model within computed, then computed will not provide a value

I'm fairly new to coding in JS and Vue.js. I've been attempting to create a dynamic search input feature that filters an array of objects fetched from my API based on user input. The strange issue I'm coming across is that the computed metho ...