What is the best way to generate a unique bootstrap card for every piece of submitted data?

I am trying to figure out how I can create a dynamic bootstrap card for each submitted data. When the form is submitted, a new card will be created and the data will be stored in that card. Each piece of data will be displayed in a column format similar to a static header (example: name, gender, etc).

Could someone please help me find a solution to this...?

  $(document).ready(function() {
    
        $("#button").click(function(e){
            e.preventDefault();
           var name = $('#name').val();
           var gender = $('input:radio[name=sex]:checked').val();
           var resident= $('input:checkbox:checked').val();
           var education = $("#selectbox").val();
    
           var content = '<td>' + name + '</td>' +
                         '<td>' + gender + '</td>' +
                         '<td>' + resident + '</td>' +
                         '<td>' + education + '</td>' +
                '<td><button class="edit-row">Edit</button><button class="delete-row">Delete</button></td>';
              
            if ($(this).hasClass('save-edit')) {
                $('.editing').html(content);
                $("#button").removeClass('save-edit').val('Submit');
            } else {
                var rowContent = '<tr class="employee-row">' + content + '</tr>';
                $('#empinfo').append(rowContent);
            }
        });
        
        $('body').on('click', '.edit-row', function (e) {
            $('.editing').removeClass('editing');
            $(this).closest('.employee-row').addClass('editing');
            $("#button").addClass('save-edit').val('Save');
        });
        
        $('body').on('click', '.delete-row', function (e) {
            $(this).closest('.employee-row').remove();
        });
    });         
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>


<form>
Name: <input type="text" id="name" value=""></input></br>
Gender : <input type="radio" name="sex" value="male">M &nbsp;&nbsp
    <input type="radio" name="sex" value="female">F</br>

Resident: <input type="checkbox" name="resident" value="Yes">Yes &nbsp;&nbsp
      <input type="checkbox" name="resident" value="No">No
</br>
Edu : <select name="selectbox" id="selectbox">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</br>
<input type="submit" id="button" class="add" value="Submit">
</form>
<div width="400" class="card w-100" id="empinfo" cellpadding="2" cellspacing="2"> 
  <div class="row">
    <div class="col-2">Name</div>
    <div class="col-2">Gender</div>
    <div class="col-2">Resident</div>
    <div class="col-2">Edu</div>
    <div class="col-2">Action</div>
  </div>
</div>

Answer №1

Here is some helpful advice. Consider changing your JavaScript td tags to div tags as shown below:

    $("#button").click(function(e){
        e.preventDefault();
       var name = $('#name').val();
       var gender = $('input:radio[name=sex]:checked').val();
       var resident= $('input:checkbox:checked').val();
       var education = $("#selectbox").val();

       var content = 
           
           '<div class="col-2">' + name + '</div>' +
                     '<div class="col-2">' + gender + '</div>' +
                     '<div class="col-2">'+ resident + '</div>' +
                     '<div class="col-2">' + education + '</div>' +
            '<div class="col-2"><button class="edit-row">Edit</button><button class="delete-row">Delete</button></div>';
          
        if ($(this).hasClass('save-edit')) {
            $('.editing').html(content);
            $("#button").removeClass('save-edit').val('Submit');
        } else {
            var rowContent = '<div class="employee-row row">' + content + '</div>';
            $('#empinfo').append(rowContent);
        }
    });
    
    $('body').on('click', '.edit-row', function (e) {
        $('.editing').removeClass('editing');
        $(this).closest('.employee-row').addClass('editing');
        $("#button").addClass('save-edit').val('Save');
    });
    
    $('body').on('click', '.delete-row', function (e) {
        $(this).closest('.employee-row').remove();
    });   

https://codepen.io/thanaphon-saeteng/pen/bGpyvee

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

Implementing a language switch feature for text display in Node.js and Next.js websites

One of the features my client is requesting for a web app is dual language support, where users can easily switch between French and English. To address this requirement, I incorporated a button that toggles a state and saves the user's language pref ...

Click to reveal the hidden div located at the bottom of the webpage

I have created a simple webpage where all the content is visible without the need to scroll down. However, there is additional content at the bottom that I want to keep hidden until the user chooses to see it. The idea is to have a phrase at the bottom o ...

Error thrown by Jest: TypeError - req.headers.get function is not defined

I have a function that is used to find the header in the request object: export async function authorizeAccess(req: Request): Promise<Response | {}> { const access = req.headers.get('Access') if(!access) return Response.json('N ...

OO Design for CSS Style Elements

I'm curious about the most effective way to implement objective-oriented CSS for quick and clean reuse of components. Scenario 1: HTML: <button class="button button-submit">Submit</button> CSS: .button { /* basic styles */ } .button ...

What are the steps to integrating D3js into a WordPress website?

After installing Wordpress and the d3js plugin, I'm looking for the most effective way to upload data in order to create and display graphs on my website. Should I directly upload csv files? And if so, where should I store them? If I prefer storing ...

Encountering an issue when bundling an application using electron-forge: "Unable to find solution for './→' "

Having trouble packaging my Electron application using electron-forge. It seems like the issue lies with native modules such as 'sqlite3'. Upon running the command npm run package, I encounter this error: Module not found: Error: Can't reso ...

Alpha Screen and Shading Filter

Having trouble with CSS filters in IE8. I have a div with a gradient background that needs to have opacity set to 0, and when hovered over, the opacity should change to 1. Here's my code: #myDiv { filter: alpha(opacity=0); opacity: 0; background:rgba ...

Having issues with custom CSS functioning properly on WordPress sites

I have a customized section on my WordPress page with specific CSS and HTML. You can view the code here: Upon hovering over the image, the code is designed to display a semi-transparent background. However, when I implement this code onto my WordPress si ...

Converting nested lists into Miller Columns: a step-by-step guide

I am interested in creating a series of interactive nested lists using Miller Columns design. The idea is that when you click on a parent item, it expands to reveal the next level of the list. While I have come across solutions that involve parsing data a ...

The setLanguage function in jsPDF does not support rendering different language characters

I'm currently working with jsPDF in Angular 2 and I'm experiencing an issue where my HTML content is not converting successfully into a PDF when it's written in Hebrew. Other languages seem to work fine, but Hebrew is causing a problem. How ...

What is the correct way to change the v-model value of a child component within a parent component

Currently, I am in the process of mastering Vue.js and I have a specific goal. I want to modify the binding value of the child component's v-model and then trigger an event in the parent component. As I delve into the Element UI documentation, I aim ...

pattern matching to establish the path for unit test files

Just starting to dive into regular expressions and I've encountered a situation in my test case config file where I need to specify certain files. var webpackConfig = require('./webpack.store1.config.js'); module.exports = function(con ...

Is there a way to utilize a POST request to pass a React component from server.js to App.js for rendering?

I am new to React and JavaScript and still in the learning process :) I'm working on a magic 8 ball application where, upon clicking the button using the post method, I aim to retrieve a random answer (one of the 20 in my server.js) back to the same ...

JavaScript Linked List Operations: Issues with the removeHead() and contains() Functions

Incorporating ES6 syntax, the challenge is to construct a linked list and subject it to an npm linter test. Below is the code implementation: class LinkedList { constructor() { this.head = null; this.tail = null; // Do not alter anything wit ...

Blur images on parent div when hovering using javascript

After some extensive searching, I came across a few helpful explanations on how to achieve my desired outcome. By combining them, I was able to get everything working smoothly with the hover effect over the image itself. However, when I attempted to trigge ...

Having trouble with changing images or background images in JavaScript?

Encountering issues with dynamic elements. When I click a button, the following JS function is executed. It displays a stylish CSS alert upon postback and then runs the function. function scrollToTop(sender, args) { window.scrollTo(0, 0); document. ...

Exploring the Difference Between Dot and Bracket Notation in Internet Explorer

I'm encountering a strange problem in IE8 when attempting to retrieve an element using the following code: window.frames.frames[0].name; // obtain the name of the inner iFrame object No complicated logic, but when the script runs, IE7-8 interprets i ...

What is the best way to retrieve the directory path from a FileReader in Java

Hey there, check out these codes I have for reading the file that the user uploads: function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#myImg' ...

Activate the Click in the Span Element with no Matching ID to the Final Segment of the URL

I'm facing an issue with triggering the click event on a specific span that lacks an id attribute. This particular span has an empty ID and doesn't respond when I try to click the back or forward buttons in the browser. All other spans trigger th ...

Execute an npm script using a gulp task

Is there a way to execute an npm script command within a gulp task? package.json "scripts": { "tsc": "tsc -w" } gulpfile.js gulp.task('compile:app', function(){ return gulp.src('src/**/*.ts') .pipe(/*execute npm run tsc*/ ...