I have divs with a common class and a hidden div to show only when said div is hovered over. Depending on the hovered div's ID it should render out a different html string in the target hidden div, append html, unhide and then hide again and clear div on mouseLeave. It seems simple enough, I'm using a switch statement to get the ID of triggering div and render the correct HTML accordingly. Console log shows the correct html string based on my switch statement, however only the last item in the switch statement gets appended to target div.
Full code is here: http://jsfiddle.net/xo1h8quq/
$( document ).ready(function(){
var buildHtml = function(id) {
console.log(id);
var html;
switch (id) {
case 'gluten':
html = '<p>This is why gluten sucks</p>';
case 'dairy':
html = '<p>This is why dairy sucks<p>';
case 'soy':
html = '<p>This is why soy sucks<p>';
case 'gmo':
html = '<p>This is why gmo sucks<p>';
case 'preservatives':
html = '<p>This is why preservatives suck<p>';
case 'fillers':
html = '<p>This is why fillers suck<p>';
case 'sugars':
html = '<p>This is why sugars suck<p>';
}
return html;
};
$( ".why-out" ).hover(
function() {
console.log(this);
var html = buildHtml(this.id);
$('#why-out-pop').html('');
//console.log(html);
$('#why-out-pop').append(html).removeClass('hide');
}, function() {
$('#why-out-pop').html('').addClass('hide');
}
);
});
.hide {
visibility: hidden;
}
#why-out-pop {
position: absolute;
margin-top: -158px;
margin-left: 30px;
border-style: solid;
border-width: 3px;
border-radius: 40px;
border-color: black;
background-color: #c3c3c3;
padding: 10px;
color: black;
text-align: right;
font-weight: light;
z-index: 1000;
}
.why-out {
text-align: right;
font-weight: bold;
}
#out-head {
text-align: right;
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test Seester</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="fuck.js"></script>
</head>
<body>
<div id="out-section">
<div class="why-out" id="out-head">WHAT'S OUT</div>
<br/>
<div class="why-out" id="gluten">Gluten/Grain /OUT</div>
<br>
<div class="why-out" id="dairy">Dairy /OUT </div>
<br>
<div class="why-out" id="soy">Soy /OUT </div>
<div class="why-out" id="gmo">GMO's /OUT </div>
<div class="why-out" id="preservatives">Preservatives /OUT </div>
<div class="why-out" id="fillers">Fillers /OUT </div>
<div class="why-out" id="sugars">Refined Sugars /OUT </div>
</div>
<div class="why-out hide" id="why-out-pop">
<p>This is a test</p>
</div>
<style>
</style>
</body>