Javascript div manipulation (Client-Side)
So I am new to javascript/jquery and I would love to have your guidance.
I started managing divs and pages by this code:
$(window).on('hashchange', function(e) // On Change Page
{
DisableAllPages(); // Disable all divs
EnablePage(); // Enable the div we specified
});
function EnablePage()
{
var fragment = window.location.hash; // Get the fragment we are in
fragment = fragment.substring(1); // remove first substring (#)
fragment = fragment.substring(1); // remove first substring (/)
$("."+fragment).css("display", "block"); // Enable div
ExecuteAccordingToPage(fragment);// Load Page functions
currentView = fragment;
main();
}
function DisableAllPages() // Disable all Divs
{
$(".content_home").css("display", "none");
$(".content_signup").css("display", "none");
$(".content_signin").css("display", "none");
$(".content_mylocation").css("display", "none");
$(".content_logout").css("display", "none");
$(".content_fivedayforecast").css("display", "none");
$(".content_query").css("display", "none");
$(".content_error").css("display", "none");
}
Basically what I'm doing is when the # changes I disable all the divs and enable the required div.
So I asked a friend and he told me that I should use this:
let currentView = null;
let main = function()
{
var htmlDiv = "#content_home"
console.log(currentView);
function init()
{
console.log(this);
this.hideCurrent();
this.show();
currentView = this;
this.bindEvents();
console.log("Inside Init");
}
function show()
{
$(this.htmlDiv).show();
console.log("Show");
}
function hideCurrent()
{
currentView.destroy();
console.log("Inside Hide");
}
function onRegisterClick()
{
alert("#some-button-id was clicked");
console.log("On Register Click");
}
function bindEvents()
{
$('#some-button-id').on('click', this.onRegisterClick )
console.log("Inside Bind Events");
}
function destroy()
{
$(this.htmlDiv).hide();
console.log("Inside Destroy");
}
}
Now would you be able to help me a bit on how to use it?
Or at least just give me your thoughts and guidance about it?
Thank you very much!