Using Javascript, I am trying to fire a function when a change is made to a select
dropdown menu.
The HTML is:
<select id="select" onchange="characterchange()">
<option value="spidey">Spider-Man
<option value="panther">Black Panther
</select>
<div id="Spidey">
<h2 style="color: red">Spider-Man</h2>
...
...
</div>
<div id="Panther">
<h2 style="color: black">Panther</h2>
...
...
</div>
The JS is:
function characterchange() {
console.log("fired characterchange");
var character = document.getElementById("select").value;
console.log(character);
if (character = "Spider-Man") {
console.log("selected spidey");
document.getElementById("Spidey").style.display = "inline";
document.getElementById("Panther").style.display = "none";
console.log("showing Spidey");
}
if (character = "Black Panther") {
console.log("selected panther");
document.getElementById("Spidey").style.display = "none";
document.getElementById("Panther").style.display = "inline";
console.log("showing Panther");
}
}
I'm getting logs for fired characterchange
, character
, selected spidey
, and selected panther
when any change is made. The CSS is not being changed and the logs that follow that code aren't firing. What am I missing?
Edit: Making the change to ===
fires the correct if statement however, my function stops after "selected spidey"
or "selected panther"
.