This is a seriously 'my first crack at HTML/JS' kind of query.
What am I trying to do?
I'm building a simple webpage where there is content that acts as a 'checklist'. eg - in my code below, you have finished 'example 1' so you click it, and onclick it should turn the background of that section a different colour.
What is going wrong
The issue I'm having is that the JS I'm using does not work when I add a CSS class/id to style it. It only works when it is a bare-bones 'li' tag.
Ironically for the sake of posting this question; when I take the script content from the separate .js file and add it to be done in the html, neither seem to work...
JS Is very much my least experienced area, would somebody have any pointers for the below code (very quick mock up of random content, not the actual page).
EDIT I forgot to add .mouseover and .selected when I originally posted this, now both are working fine on the 'run code snippet' on here and not on the page... it's no doubt something I've done on the organization of the actual project.
Thanks to all who have replied!! :)
//COLOUR CHANGE ON-CLICK AND MOUSE HOVER
$(document).ready(function() {
$('li').on('click', function() {
$(this).toggleClass('selected');
});
$('li').on('mouseenter', function() {
$(this).toggleClass('mouseover')
});
$('li').on('mouseleave', function() {
$(this).toggleClass('mouseover')
});
});
.container {
background-color: #0375B4;
margin-bottom: 20px;
}
.listitem {
border: 1px;
background: #E2F2FF;
margin: 10px;
padding: 9px;
font-size: 23px;
list-style-type: none;
}
/* EDIT : I missed this off on my original post yesterday sorry! */
/* shadow effect when hovering over list items */
.selected {
background: #a7ff7f;
}
.mouseover {
box-shadow: 0 7px 8px 0 rgba(0, 0, 0, 0.2), 0 8px 20px 0 rgba(0, 0,
0, 0.19);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col">
<ul>
<li class="listitem">example 1</li>
<li class="listitem">example 2</li>
<li class="listitem">example 3</li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<ul>
<li>example 1</li>
<li>example 2</li>
<li>example 3</li>
</ul>
</div>
</div>
</div>
selectedandmouseoverdefined? Chances are, thelistitemclass is defined in your stylesheet much later and therefore overrides the styles applied by theselectedclass.