Reputation: 85
So i am tryign to make a timeline, where if you select an item from a dropdown selection box it will display only the information from that selection (era). I found this Code which lets me do just that, but once i added a few thigns of my own, it stopped working. i was wondering why this is happening, and what could be done to fix it. Full Code:
<script type="text/javascript">
$(document).ready(function(){
$("select").change(function(){
$(this).find("option:selected").each(function(){
if($(this).attr("value")=="red"){
$(".box").not(".red").hide();
$(".red").show();
}
else if($(this).attr("value")=="green"){
$(".box").not(".green").hide();
$(".green").show();
}
else if($(this).attr("value")=="blue"){
$(".box").not(".blue").hide();
$(".blue").show();
else if($(this).attr("value")=="maroon"){
$(".box").not(".maroon").hide();
$(".maroon").show();
else if($(this).attr("value")=="magenta"){
$(".box").not(".magenta").hide();
$(".magenta").show();
}
else{
$(".box").hide();
}
});
}).change();
});
</script>
</head>
<body>
<div>
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="maroon">maroon</option>
<option value="magenta">magenta</option>
</select>
</div>
<div class="red box">
</div>
<div class="green box">hello</div>
<div class="blue box">talofa</div>
<div class="maroon box"> <h2>BURGANDY!</h2></div>
<div class="magenta box"> <h2>PINK!</h2></div>
EDIT: I am extremely new to JS and JQuery (Like, Never used either of them before), so explanations are appreciated!
Upvotes: 3
Views: 13743
Reputation: 178413
Other answers explained the typo.
Here is a simpler version
Fixed the code and added initial hide all css
$(function() {
$("select").on("change", function() {
const val = this.value
$(".box").not("." + val).hide();
$("." + val).show();
}).change();
});
.box {
display: none;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
.maroon {
background-color: maroon;
}
.magenta {
background-color: magenta;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="maroon">maroon</option>
<option value="magenta">magenta</option>
</select>
</div>
<div class="red box">Red</div>
<div class="green box">Green</div>
<div class="blue box">Blue</div>
<div class="maroon box">
<h2>Maroon!</h2>
</div>
<div class="magenta box">
<h2>Magenta!</h2>
</div>
Upvotes: 0
Reputation: 379
Try this:
$(document).ready(function(){
// Use className or Id instead of direct tag name
$('.choose-color').on('change', function() {
var val = $(this).val();
var box = $('.box');
box.hide(); // hide all boxes
$('.' + val).show(); // show the current one
})
});
Upvotes: -1
Reputation: 4757
You have several syntax
errors . You are not closing the else if
with }
. You may want to check console
for those.
Also, not sure why you are doing $(this).find("option:selected").each(function(){...
since there can be only one option selected at any given time.
Here is the working code.
$(document).ready(function() {
$("select").change(function() {
var color = $(this).val();
if (color == "red") {
$(".box").not(".red").hide();
$(".red").show();
} else if (color == "green") {
$(".box").not(".green").hide();
$(".green").show();
} else if (color == "blue") {
$(".box").not(".blue").hide();
$(".blue").show();
} else if (color == "maroon") {
$(".box").not(".maroon").hide();
$(".maroon").show();
} else if (color == "magenta") {
$(".box").not(".magenta").hide();
$(".magenta").show();
} else {
$(".box").hide();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="maroon">maroon</option>
<option value="magenta">magenta</option>
</select>
</div>
<div class="red box">
</div>
<div class="green box">hello</div>
<div class="blue box">talofa</div>
<div class="maroon box">
<h2>BURGANDY!</h2>
</div>
<div class="magenta box">
<h2>PINK!</h2>
</div>
Upvotes: 4
Reputation: 99041
You're missing some closing brackets near the else's. Find a good javascript IDE, something like sublime, also make sure you read the console messages.
$(document).ready(function(){
$("select").change(function(){
$(this).find("option:selected").each(function(){
if($(this).attr("value")=="red"){
$(".box").not(".red").hide();
$(".red").show();
}else if($(this).attr("value")=="green"){
$(".box").not(".green").hide();
$(".green").show();
}else if($(this).attr("value")=="blue"){
$(".box").not(".blue").hide();
$(".blue").show();
}else if($(this).attr("value")=="maroon"){
$(".box").not(".maroon").hide();
$(".maroon").show();
}else if($(this).attr("value")=="magenta"){
$(".box").not(".magenta").hide();
$(".magenta").show();
}else{
$(".box").hide();
}
});
}).change();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<select>
<option>Choose Color</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="maroon">maroon</option>
<option value="magenta">magenta</option>
</select>
</div>
<div class="red box">Redish!</div>
<div class="green box">hello</div>
<div class="blue box">talofa</div>
<div class="maroon box"> <h2>BURGANDY!</h2></div>
<div class="magenta box"> <h2>PINK!</h2></div>
Upvotes: 2