I'm taking my first steps in JQuery as I have an event I need to deal with on the website I'm trying to put together. All of this is just for training purposes.
I've taken a (free) bootstrap theme for an e-commerce website and am trying to apply some JQuery events to it to learn how it all hangs together. Some things are working well and others not at all. My goal here is to handle the event of a dropdown list (select) and reload a div element based on the result. So far I have not been able to either act ON the list or process events FROM it.
The select is contained in the div below. There is a label called "sortby" and the select, called "sortorder".
<div class="shop-top">
<div class="shop-shorter">
<div class="single-shorter">
<label id = "sortby">Sort By :</label>
<select name="sortorder" id="sortorder">
<option selected="selected">Name</option>
<option>Barcode</option>
<option>Price</option>
</select>
</div>
</div>
</div>
These are the three sample scripts I'm trying to get working to understand JQuery a bit. I've put them all in the head section and included jquery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
$("#sortby").click(function(){
$("#sortby").hide();
});
});
</script>
This one works fine. I click on the label and the label disappears.
<script type="text/javascript" >
$(document).ready(function(){
$("#sortby").click(function(){
$("#sortorder").hide();
});
});
</script>
This one does not work. I click on the label and the select list does not disappear.
<script type="text/javascript" >
$("#sortorder").change( function () {
var option = $("#sortorder").val();
if(option.toLowerCase() == "name"){
alert("name");
}
});
</script>
This one does not work. I select the option 'name' from the select list and there is no alert message displayed.
On the page itself, if I inspect the dropdown list I get the following:
<div class="nice-select" tabindex="0"><span class="current">Name</span><ul class="list"><li data-value="Name" class="option selected">Name</li><li data-value="Barcode" class="option">Barcode</li><li data-value="Price" class="option">Price</li></ul></div>
This "nice-select" is from a .css file which was included in the bootstrap theme, it's not explicitly mentioned in the file where the above code is from. The above block is preceded by the actual code in the element inspection window
<select name="sortorder" id="sortorder" style="display: none;">
<option selected="selected">Name</option>
<option>Barcode</option>
<option>Price</option>
</select>
So,two things - I don't understand the mechanism whereby the select element is duplicated by this class and it seems as though this duplicated code is what is actually usable on the site. And, how do I actually interact with the element in question?
Edit: I've decided to park this with an intermediate solution for now. I've just added a button which does (part of) the job of retrieving the value of the select list when clicked.
$(document).ready(function() {
$("#apply").on("click", () => {
var sortType = $('#sortorder').val();
if (sortType == 'Name') {
$("#product-grid").load
alert("sort by name");
} else {
$("#product-grid").load
alert("sort by price");
}
});
});
For the sake of completion, here's the code that does not work
$(document).ready(function() {
/*$("#sortorder").niceSelect();
$("#sortorder").on("change", () =>{
var selected = $("#myselect").val();
if (sortType == 'Name') {
$("#product-grid").load
alert("sort by name");
} else {
$("#product-grid").load
alert("sort by price");
}
})*/