menu

Questions & Answers

Why don't my created element apply the CSS style?

On my project I have a function which simply add a element on the body.

function addListItem() {
    item = document.createElement('div')
    item.classList.add('item')

    checkboxdiv = document.createElement('div')
    checkboxdiv.className = "check-box"

    check_box_path = document.createElement('label')
    check_box_path.className = "checkbox path"

    checkbox = document.createElement("input")
    checkbox.type = "checkbox"

    svg = document.createElementNS("M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186", "svg")
    svg.setAttribute("viewBox", "0 0 21 21")

    path = document.createElementNS("M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186", "path")
    path.setAttribute("d", "M5,10.75 L8.5,14.25 L19.4,2.3 C18.8333333,1.43333333 18.0333333,1 17,1 L4,1 C2.35,1 1,2.35 1,4 L1,17 C1,18.65 2.35,20 4,20 L17,20 C18.65,20 20,18.65 20,17 L20,7.99769186")

    background = document.createElement("div")
    background.className = "background" 

    h1 = document.createElement("h1")
    h1.textContent = "Insira seu texto aqui"

    //svg

    svg.appendChild(path)

    //label

    check_box_path.appendChild(checkbox)
    check_box_path.appendChild(svg)

    //check-box

    checkboxdiv.appendChild(check_box_path)

    //background
    background.appendChild(h1)

    //item

    item.appendChild(checkboxdiv)
    item.appendChild(background)


    list = document.getElementById("list")
    return list.appendChild(item)
}

This item has a checkbox which I just took a copy of the css from another website (I'm gonna let the credits after), but the item created by the function just doesn't apply the style when it is checked.

Putting the item on HTML file, its works well how should work.

here the checkbox css

    .checkbox {
    --background: #fff;
    --border: #D1D6EE;
    --border-hover: #BBC1E1;
    --border-active: #1E2235;
    --tick: #fff;
    position: relative;
    input,
    svg {
        width: 21px;
        height: 21px;
        display: block;
    }
    input {
        -webkit-appearance: none;
        -moz-appearance: none;
        position: relative;
        outline: none;
        background: var(--background);
        border: none;
        margin: 0;
        padding: 0;
        cursor: pointer;
        border-radius: 4px;
        transition: box-shadow .3s;
        box-shadow: inset 0 0 0 var(--s, 1px) var(--b, var(--border));
        &:hover {
            --s: 2px;
            --b: var(--border-hover);
        }
        &:checked {
            --b: var(--border-active);
        }
    }
    svg {
        pointer-events: none;
        fill: none;
        stroke-width: 2px;
        stroke-linecap: round;
        stroke-linejoin: round;
        stroke: var(--stroke, var(--border-active));
        position: absolute;
        top: 0;
        left: 0;
        width: 21px;
        height: 21px;
        transform: scale(var(--scale, 1)) translateZ(0);
    }
    &.path {
        input {
            &:checked {
                --s: 2px;
                transition-delay: .4s;
                & + svg {
                    --a: 16.1 86.12;
                    --o: 102.22;
                }
            }
        }
        svg {
            stroke-dasharray: var(--a, 86.12);
            stroke-dashoffset: var(--o, 86.12);
            transition: stroke-dasharray .6s, stroke-dashoffset .6s;
        }
    }
    &.bounce {
        --stroke: var(--tick);
        input {
            &:checked {
                --s: 11px;
                & + svg {
                    animation: bounce .4s linear forwards .2s;
                }
            }
        }
        svg {
            --scale: 0;
        }
    }
}

@keyframes bounce {
    50% {
        transform: scale(1.2);
    }
    75% {
        transform: scale(.9);
    }
    100% {
        transform: scale(1);
    }
}

html {
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
}

* {
    box-sizing: inherit;
    &:before,
    &:after {
        box-sizing: inherit;
    }
}

// Center & dribbble
body {
    min-height: 100vh;
    display: flex;
    font-family: 'Roboto', Arial;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background: #F6F8FF;
    .grid {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        grid-gap: 24px;
    }
    .dribbble {
        position: fixed;
        display: block;
        right: 20px;
        bottom: 20px;
        img {
            display: block;
            height: 28px;
        }
    }
    .twitter {
        position: fixed;
        display: block;
        right: 64px;
        bottom: 14px;
        svg {
            width: 32px;
            height: 32px;
            fill: #1da1f2;
        }
    }
}

Checking the inspector, I can see the created element just doesn't apply the check style when its checked.

Comments:
2023-01-07 20:29:44
Please add the css to your question. stackoverflow.com/help/minimal-reproducible-example
2023-01-07 20:29:44
The CSS you posted isn't valid CSS, but rather SCSS, which needs to be compiled to CSS.
Answers(0) :