Today I needed to do some javascript, I have been developing in Blazor as of late, and my head has been all C# I genuinly forgot how to do javascript for jquery. Usually I would google this, so lets see what the search engine returns for my question:
"a js function that handles onclick of dropdown-menu and reads the data-mode attribute"
Then I decided to ask Git CoPilot, and this is what it returned:
Below is the javascript code snippet
$(document).ready(function() {
$('.dropdown-menu').on('click', '.dropdown-item', function(event) {
event.preventDefault(); // Prevent the default action
var mode = $(this).data('mode'); // Read the data-mode attribute
console.log('Mode selected:', mode);
// You can add more logic here to handle different modes
});
});
I realised at this point that I also needed to do a GET request and get some Json, so I modified my question to Git:
"a js function that handles onclick of dropdown-menu and reads the data-mode attribute and does a json get using the url in the data-url attrib"
This javascript code is exactly what I needed:
$(document).ready(function() {
$('.dropdown-menu').on('click', '.dropdown-item', function(event) {
event.preventDefault(); // Prevent the default action
var mode = $(this).data('mode'); // Read the data-mode attribute
var url = $(this).data('url'); // Read the data-url attribute
console.log('Mode selected:', mode);
console.log('Fetching data from URL:', url);
$.getJSON(url, function(data) {
console.log('Data received for mode', mode + ':', data);
// You can process the received JSON data here based on the mode
}).fail(function() {
console.error('Error fetching data from', url);
});
});
});