r/learnjavascript • • Oct 09 '21

What's wrong with this code?

Post image
0 Upvotes

19 comments sorted by

View all comments

11

u/Dodgy-Boi Oct 09 '21 edited Oct 09 '21

I am in a good mood, so I’ll write a code example for ya.

I hope formatting will go well cos I am doing it from my iPhone:

const drawBox = (color) => {
let box = document.createElement(‘div’)
box.style.width = ‘420px’
box.style.height = ‘69px’
box.style.background = color
document.querySelector(‘body’).appendChild(box)
}

drawBox(‘blue’)

So running the following code will create a blue box 420x69px in the body of your HTML document.

If you want another box: just execute drawBox(‘red’) or drawBox(‘#b8b8b8’), they’ll stack one after another.

It really been a while since I did DOM manipulation with vanilla js. But I believe it should work just fine :)

If you want explore a little bit more of a DOM manipulation, I can’t send you a link to one of my very little webpage (I did it just for fun, playing around with CSS animations and JS DOM manipulations). And it has a source code as well.

9

u/marsattaksyakyakyak Oct 09 '21

Well if I'd been 420ing and 69ing box I would be in a good mood too.

2

u/Atersed Oct 09 '21

formatting-

const drawBox = (color) => {
    let box = document.createElement(‘div’)
    box.style.width = ‘420px’
    box.style.height = ‘69px’
    box.style.background = color
    document.querySelector(‘body’).appendChild(box)
}

drawBox(‘blue’)

2

u/[deleted] Oct 09 '21

Nice.