Change color of a div using JS

Asked

Viewed 12,539 times

2

I am trying to make a div element change background color when selecting the color of my input using Js. I’m not getting anybody to help me ?

<style>
    .corExemplo {
        width: 150px;
        height: 150px;
        border-radius: 50%;
        margin-bottom: 30px;
        float: left;
    }
</style>

<script>
    function trocaCor(){
        var cor = getElementById("corum").value;
        document.getElementById("boxum").style.backgroundColor = cor;
    }
</script>

<div id="boxum" class="corExemplo">Teste</div>

<form action="#" style="clear: both;">
    <label for="corum">Cor 1</label>
    <input type="color" id="corum" onchange="trocaCor();">
</form>

2 answers

2


Only the document. prefixing getElementById("corum").value;

See below the code working:

function trocaCor(){
   var cor = document.getElementById("corum").value;
   document.getElementById("boxum").style.backgroundColor = cor;
}
    .corExemplo {
        width: 150px;
        height: 150px;
        border-radius: 50%;
        margin-bottom: 30px;
        float: left;
    }
<div id="boxum" class="corExemplo">Teste</div>

<form action="#" style="clear: both;">
    <label for="corum">Cor 1</label>
    <input type="color" id="corum" onchange="trocaCor();">
</form>

  • Wow I didn’t even notice... I’ve looked at the whole code over and over again. Thank you very much!

  • We’ve all been through this countless times!!!! It worked?

  • Yes, as I wanted! I’m starting now in Js, not being very easy rsrsrsr

  • By any chance would you mark as answered ? because I can only do it in a couple of minutes

  • I could use a little help on another code ?

  • Sure, send the link to the other question.

  • http://answall.com/questions/149828/estou-tentando-alterar-a-borda-da-div-e-ao-mesmo-tempo-mostrar-o-valor-em-outra

Show 2 more comments

1

To allan’s response works perfectly. I’ll just add an example with the addeventlistener for knowledge purposes.

You can use the Element.addEventListener() to "wait for an event" and change the color.

For your example I would consider something unnecessary, but as I said it is starting in the world of JavaScript now, I think it would be something very interesting for you to see.

Your example using this method would look like this:

var cor = document.getElementById("corum");

cor.addEventListener("change", function() {
    document.getElementById("boxum").style.backgroundColor = cor.value;
}, false);
.corExemplo {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  margin-bottom: 30px;
  float: left;
}
<div id="boxum" class="corExemplo">Teste</div>

<form action="#" style="clear: both;">
    <label for="corum">Cor 1</label>
    <input type="color" id="corum">
</form>

That way, I add the event change() at the input for addEventListener(), and not directly on input.

  • Wow, I didn’t know this way... Very cool, if I put an external Js the code gets cleaner ? In this case, there in place of "change", it needs to be this event ?

  • @Victor It is common to separate JS from HTML, but it is not a rule. About change, you need to pass a valid to what is expected. If you place the event input will also work. This approach is widely used when developing libs, because it needs something dynamic and can not do "mooring" directly in HTML, because you do not know how will be the code that will use.

  • Got it, thanks for the help, I’m reading mt this page from Mozilla, there is js thing mt there, before I was studying by iMasters but I got a little lost when I entered the POO, and I’m starting dnv rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.