Why don't HTML elements added to the DOM using JS have Bootstrap properties?

Sonjay

I am working on a project which uses bootstrap to create various nested components which are styled according to Bootstrap's class system. I have already created what I deem a successful card component using bootstrap, but when trying to clone this element using the exact same classes and layout using JS and the DOM system, the appended card component has HTML default styles rather than bootstrap's styles. The classes and nesting are exactly the same.

I tried combing through my code line by line and making sure that the layout was an exact duplicate. Here is an example of my base HTML code not produced by JS:

<div class="row bookContainer">
            <div class="col-md-4">
                <div class="card">
                    <div class="card-body">
                        <div class="card-title">Title Here</div>
                        <div class="card-text">
                            <p>By Mr. Ipsum Dolor</p>
                            <p>Pages: 2000</p>
                            <button type="button" class="btn btn-outline-success  complete">Completed</button>
                            <button type="button" class="btn btn-outline-danger">Remove</button>

                        </div>
                    </div>
                </div>

            </div>

        </div>

and an accompanying image of the card it produced:

Successful component.

I then used the following JS code to produce the a clone of the element layout and add it to the DOM:

Step 1: Converting book object to html

function bookToCard(book) {
    //col div
    const colDiv = createDiv();
    colDiv.classList.add("col-md-4");

    //card div
    const card = createDiv()
    card.classList.add("card");

    //card body
    const cardBody = createDiv();
    cardBody.classList.add("card-body");

    //card title
    const cardTitle = createDiv();
    cardTitle.classList.add("card-title");


    //adding title content
    let titleContent = createText(book.title);
    cardTitle.appendChild(titleContent);
    cardBody.appendChild(cardTitle);


    //card text and content
    const cardText = createDiv();
    cardText.classList.add("card-text");

    let authorParagraph = document.createElement("p");
    authorParagraph.appendChild(createText(book.author));

    cardText.appendChild(authorParagraph);

    let numParagraph = document.createElement("p");
    numParagraph.appendChild(createText(book.pages));

    cardText.appendChild(numParagraph);

    //buttons
    let cButton = document.createElement("button"); //complete or incomplete button
    cButton.classList.add("btn");
    if (book.completed) {
        cButton.classList.add("btn-outline-success");
    } else {
        cButton.classList.add("btn-outline-secondary");
    }
    cardText.appendChild(cButton);

    let rButton = document.createElement("button");
    rButton.classList.add("btn", "btn-outline-danger");
    cardText.appendChild(rButton);

    cardBody.appendChild(cardText);

    card.appendChild(cardBody);

    colDiv.appendChild(card);

    return colDiv;
}

Step 2: updating HTML display

function updateDisplay() {
    let container = document.querySelector(".bookContainer");
    container.innerHTML = '';
    container.classList.add("row", "bookContainer");
    for (let i = 0; i < library.length; i++) {
        container.appendChild(bookToCard(library[i]));
    }

}

The process results in the following element being added to the page (with the successful version shown at left):

enter image description here.

Any help on how to get the right card to have Bootstrap properties would be appreciated. Sorry for the sloppy code.

mohsen dorparasti

Based on the images, the issue is not the styling, as bootstrap classes have been applied to the new card. The issue is you forgetting to add text to created <button>s and <div>s.

For cButton

let cButton = document.createElement("button"); 
cButton.classList.add("btn");
if (book.completed) {
    cButton.classList.add("btn-outline-success");
    cButton.textContent = "Completed"; // <<<<<<<<<<<<<< here
} else {
    cButton.classList.add("btn-outline-secondary");
    cButton.textContent = "In Progress"; // <<<<<<<<<<<<<< and here
}

And for author

let authorParagraph = document.createElement("p");
authorParagraph.appendChild("By " + createText(book.author));  // <<<<< added "By "

Need to do the same for pages and Remove

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Vue.js DOM added after initialization don't interact

How do I loop through an array and select elements that don't have index value using JS

Why don't all elements with the same class have the same padding?

Swift - why elements don't have equal sizes?

Why I don't have access to generic type class elements?

Why don't the pills in Bootstrap 4 have any style?

D3.js: Elements don't have any width

Javascript button that changes properties of html elements using DOM Events

Using BeautifulSoup to scrape elements that don't have unique identifiers

Why don't HTML templates that I have parsed work?

Why don't some websites have a .html extension in their pages?

Why don't you have to dereference a pointer using strcpy and strlen?

Why I can't check if an HTML element (retrieved from the DOM) have set a specific CSS class using JQuery?

Why does a directive have to be added to a HTML tag?

Objects added dynamically to the DOM don't respond to jQuery Events

Some css properties set by jquery don't show up on html elements

Do HTML elements have 'hidden indexes' for the DOM?

Why do some UI elements in Unity have an anchor point feature and others don't?

How to add html i tag to sub-strings that matched the elements of the array if they don't already have the tag

Why don't JavaScript properties update automatically?

Android checkboxes added by code don't have the right appearance?

Angular's bindings don't work with elements added in directives

Subsequent RewriteRules don't transform elements added in previous transform

Why should HTML DOM properties be reflected into HTML DOM attributes as well?

Accessing elements added to DOM with ng-bind-html

Align in a straight row of DOM elements using bootstrap

Why don't .not() have any affect?

Why don’t routers have a shutdown process?

Why I don't have there NotSerializableException?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive