Setting a input type color to a ja variable

Question:
I have a <p>Choose your color: <input type="color" id="colorInput"></p> and I want to set It as a js variable then use document.getElementById() to set the color chosen to the background color of <h1 class='title-main' onclick='editTitle()' id='title'>Welcome to your JohnnySite</h1>
Repl link:
https://replit.com/@JohnnySuriano/JohnnySites#index.html)

(This is the second time is made this post because the other time it glitched and broke)

You can do this:

<p>Choose your color: <input type="color" id="colorInput" onchange="changeBack(this);"></p>
    ...
    ...
<script>
    function changeBack(color_input) {
        var color = color_input.value;
        document.getElementById('title').bgcolor = color;
};
</script>
7 Likes

It may be more efficient to use document.getElementById('title').style.backgroundColor = color but Iā€™m not sure.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.