Activate an event on a separate webpage using jQuery or JavaScript

Currently, I am in the process of working on a project with 2 distinct pages:

  1. Index
  2. Settings

On the Settings page, I have implemented a button to close an element and hide it. However, I am encountering an issue where I want the elements on the Index page to be hidden when I click on close in Settings and then select save. Unfortunately, I have been unsuccessful in achieving this functionality so far. Any assistance or guidance on how to accomplish this would be greatly appreciated.

If you would like to review the code I have developed for this scenario, please visit: http://codepen.io/crazycoder775/pen/pNYJOw

 $(".list_sbar li").click(function(e) {
    if ($(this).outerWidth() - 34 <= e.offsetX)
        $(this).remove();
});
$(".list_sbar li").click(function(e) {
    if ($(this).outerWidth() - 34 <= e.offsetX)
        $(this).remove();
});
$(document).ready(function(){
    $("#hide").click(function(){
        $(".test1").hide();
    });
    $("#show").click(function(){
        $(".test1").show();
    });
});
$(document).ready(function(){
    $("#hide2").click(function(){
        $(".test2").hide();
    });
    $("#hide3").click(function(){
        $(".test3").hide();
    });
});

Within the provided code snippet, there are 3 divs and 2 close buttons. By clicking on any of the close buttons, the corresponding div will be appended with a hide class.

Answer №1

To create a dynamic element that can be hidden or revealed based on certain conditions, you can utilize a combination of a cookie value and the $(window).focus event listener. This setup will allow you to control the visibility of the element when the user interacts with the page.

If you need more information on how cookies work in web development, refer to this documentation: https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

Follow these steps to implement the functionality:

  $(document).ready(function(){
    if( document.cookie.indexOf('element_closed=')== -1){
        document.cookie = 'element_closed=false; path=/';
     }
    
    function getCookie(name) {
      var value = "; " + document.cookie;
      var parts = value.split("; " + name + "=");
      if (parts.length == 2) return parts.pop().split(";").shift();
    }

    function hideElement(){
      $(".toggle_target").hide();
      document.cookie = 'element_closed=true; path=/'
      console.log(document.cookie);
    }

    $(window).focus(function(){
      console.log(getCookie('element_closed'));
      if(getCookie('element_closed') == 'true'){
        hideElement();
      }
    });

    $(".toggle").on('click', function(e){
      e.preventDefault();
      hideElement();
    });
  });

For a live demonstration, check out this jsfiddle link: https://jsfiddle.net/b1nyczht/20/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to utilize an Event Emitter to invoke a function that produces a result, and pause until the answer is provided before continuing?

Looking for a way to emit an event from a child component that triggers a function in the parent component, but with a need to wait for a response before continuing. Child @Output() callParentFunction = new EventEmitter<any>(); ... this.callParen ...

What is the best way to create a slideshow that automatically starts upon loading and pauses when hovered over?

I have recently set up a div element for my slideshow. I've included a script to enable navigation using next and previous arrows. However, I am looking to add an automatic slideshow feature that stops on hover. As a beginner, I would appreciate any a ...

Sending Data to PHP Script Using AJAX Without Submitting the Form

Seeking assistance with capturing onclick event for image deletion. I have a code snippet below that triggers an 'onclick' event for deleting images which calls the 'delete.php' script to remove physical images from the server. <sc ...

Transforming button alignment from vertical to horizontal in Bootstrap based on the screen size of the device

My website features a series of vertically aligned buttons on the sidebar, styled using the following CSS: CSS: .sidebar { position: fixed; left: 20px; top: 5%; width: 120px;} .sidebar .icons { font-size: 30px; margin: 21px; } ...

Issue with React-Toastify not displaying on the screen

After updating from React-Toastify version 7.0.3 to 9.0.3, I encountered an issue where notifications are not rendering at all. Here are the steps I followed: yarn add [email protected] Modified Notification file import React from "react" ...

Issues persist with Ajax form submissions; the submitted data never seems to go through

I have encountered variations of this issue multiple times, but despite analyzing numerous examples, I am unable to determine why my code is not functioning properly. <script> $('document').ready(function(){ $('datafixForm' ...

Learn to Generate a Mathematical Quiz with Javascript

For a school project, I am tasked with developing a Math Quiz which showcases questions one at a time. The questions vary in type, including Multiple Choice, Narrative Response, Image Selection, Fill in the blank, and more. I require assistance in creatin ...

Having trouble with CSS transitions not showing up correctly in Chrome. Any suggestions on how to resolve this issue?

While working on a static website, I attempted to apply a background-color transition to a button. Despite reviewing my code multiple times, I discovered that the issue was not with my coding. It seems like the browser is having trouble displaying CSS tran ...

Guide on creating frozen columns in an Angular 2 table with the ability to scroll through multiple columns

Within my table, there are 3 columns that must always remain on the left side. Additionally, there is a column containing a grid where each element represents an hour of the day, requiring it to be scrollable. I have attempted various proposed solutions, ...

Node.js: Capturing requests and responses for console logging

I'm currently working on a Hapi server using Good to log all requests and responses to the console. I've been able to successfully log responses but I'm facing issues with logging the body of the response, and for requests, I'm not gett ...

Styling tables within HTML emails for Gmail and then utilizing PHPMailer to send the emails

I've been racking my brain over this for hours with no luck! Objective: Implementing inline styles for table, td, th, p elements in an HTML email intended for Gmail using PHPMailer. Challenge: Inline styles not being rendered HTML Snippet: <sec ...

In what way can I fill out my search fields using the criteria included in the URL?

In the process of developing an asp.net web application, I have implemented a code snippet to construct a section for search fields. This code builds URL parameters based on user input: <script type="text/javascript> $(document).ready(function(){ ...

Creating a card design that responds to user interactions using CSS and HTML

I attempted to create a responsive queue of playing cards that adjusts based on the display width. I want this queue to perfectly fit the display width so that no card is cut off. It should be optimized for phones, tablets, and desktop screens. Additiona ...

JavaScript function that shows the name of an individual extracted from a specific file

Hey there, currently I'm diving into a javascript tutorial and exploring ways to display a person's name along with their birthday on this historical day. <script type="text/javascript"> document.write("Today is <br/&g ...

What is a reliable method for consistently updating backend C# with user-side JS data whenever it is altered?

I'm working on a front end JS application that includes visual sliders. I need to send the updated slider information to the C# backend (ASP.NET) whenever a user makes changes. After some research, it seems like AJAX is the most efficient way to achie ...

Accessing a JSON array from PHP script with the help of jQuery

Recently, I delved into the world of jQuery and PHP by attempting to pass variables from Javascript to a PHP script using $.ajax. Despite my lack of expertise in this area, I managed to write some code that kind of worked. However, today I encountered a n ...

The edit functionality in jqGrid does not function properly if custom search parameters are designated

Using the Guriddo jqGrid JS version 5.2.0 implemented here: @license Guriddo jqGrid JS - v5.2.0 - 2016-11-27 Copyright(c) 2008, Tony Tomov, [email protected] The code block below showcases an entire self-contained implementation of jqGrid. It inclu ...

Unable to show the table row color using Angular in Internet Explorer

I've customized a table by changing the row color based on a property value, and I'm using ng-repeat to display the rows. Here's my table: <table class="tab table span6 tabhover" style="margin:0;" > <thead> ...

Regarding passing input into a JavaScript class method that is exported through the exports keyword

The inquiry at hand relates to ExtendScript code, however, I believe it should be independent of any specific javascript implementation. If we have the following in a JS library file (base64.js) exports.encode64 = encoder('+/'); //... function ...

Receiving JSON output twice

I am currently working with CodeIgniter and facing an issue with my form fields, which are Employee_name, fromDate, and endDate. I am using AJAX to send this data without sharing the actual code. The problem arises when retrieving and displaying records fr ...