+ 14
I have a doubt.
I want to create a background color app. In which when we enter a color name by user input. the background color should be changed to the color entered. I don't know what should I use in js to do that. I have a few options. please choose one and answer. 1) document.body.style.backgroundColor.getElementbyId="" 2)document.getElementbyId 3)other comment below
4 odpowiedzi
+ 8
first make an input tag and button tag in html and grab the button in javascript:
HTML:
<input id="color-input" />
<button id="submit-btn">Submit</submit>
JS:
let submitBtn = document.getElementById("submit-btn")
Then define that what happens after you click on the button
JS:
submitBtn.onclick = function() {
// NOW grab the input value to get the latest one
let color = document.getElementById("color-input").value
// be sure to put a .value otherwise you would get a whole tag instead of the value.
docment.body.style.backgroundColor = color
}
+ 8
thnk u every one
+ 3
the below code selects the first input element in your html page ,and listens for input event ,whenever anything is inputted it assign the value to the desired element background color ,
document.querySelector("input").oninput=(e)=>{
document.getElementById("").style.backgroundColor=e.target.value
}
+ 2
Make code from any options that you want