AngularJS: Implementing a toggle click function for a list of items within an iframe

Here's the workflow I'm dealing with:

I have a collection of items, each having an ng-click event that triggers the display of an iframe below the clicked item. The process works like this:

  • When clicking an item, a hidden div tag beneath it appears, containing an iframe.
  • If another item is clicked from the same list, a new div tag shows up below that item, displaying the iframe for the most recently clicked item.

The main challenge I'm facing right now is:

If there is already an open div tag with an iframe when clicking on a new item, the existing div gets closed before showing the new one along with its iframe.

Currently, if an iframe is already open and I click on another item, a new iframe opens without closing the previous one until I interact with it directly.

How should I handle this situation?

PS: I hope the scenario is clear, do let me know if you need more clarification.

Edit-

Sharing a jsfiddle link is quite challenging as this question is part of a large project folder I'm currently working on. Nevertheless, I'll include the code here in the hopes that it will suffice.

HTML:

<div ng-repeat="data in JsonData">
    <div class="row" ng-click="getGraph(data.id, $index)">
        <div class="col-lg-6 text-left">
            <span id="title">Title: &nbsp;{{data.name}}</span><br><br>
            <span style="color: #000 !important">
                <strong>Id:</strong> &nbsp;{{data.id}}
            </span><br/><br>
        </div>
        <div class="col-lg-4 text-left" style="color: #000 !important;">
            <span style="text-align: left; font-size: 13px;">
                  <strong>Details:</strong> {{data.details}}
            </span><br>
        </div>
        <div class="iframe col-lg-10 col-lg-offset-2">
            <div class="ibox float-e-margins ibox_shadow">
                <iframe style="width: 80%; height: 50%; id="targetframe" name="targetframe" allowTransparency="true" scrolling="no" frameborder="0" ng-src="{{graphUrl | trustAsResourceUrl}}">
                </iframe>
            </div>
        </div>
    </div>
</div>

And the controller.js code:

$scope.getGraph = function(d,i) {
        $scope.graphUrl = 'http://server-url.com:8888/path/to/theGraph?id='+d;
        var elem = document.getElementsByClassName(d);
        $(elem).toggleClass('class_to_toggle_hide_show');
}

Answer №1

By sharing the code, it will help clarify things better. In the ng-click event, you can start by using ng-click="yourFunction($event)" and

$scope.yourFunction = function(event){
   $(even.target).find("iframe").remove();
   //add your additional logic here

}

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

Protecting an AJAX interface against unauthorized exploitation by external websites

We are in the process of creating a website that utilizes a basic JSON API (RoR) for displaying information on the page. This data is accessible to our clients, but crucial to our service, so we are taking precautions to prevent competitors from accessin ...

The NestJS HttpException class will default to a status code of 201 if no specific status code is

This particular instance showcases how instantiating the HttpException class in this manner results in an exception being thrown with an undefined status, ultimately becoming a status of 201 (I presume it defaults to this status as it is associated with a ...

The necessity of utilizing a dummy object for storing events in JavaScript is evident in certain situations

I am confused as to why in some instances a dummy object is needed in JavaScript to store events, like in the following code: Metal.Gold = function() { var temp = $("<div>"); //dummy object this.Submit = function(url1, method, data){ ...

Tips for creating a gradual fade-out effect on a bootstrap modal window

One issue I'm facing is with my modal dialog (#busyIndicator). It simply displays a message that reads "Please Wait". Sometimes, the operation it's tied to completes so quickly that the visual transition between showing and hiding the dialog beco ...

Embracing async-await while awaiting events in node.js

I am attempting to incorporate async await into a project that is event-driven, but encountering an error. The specific error message I am receiving is: tmpFile = await readFileAsync('tmp.png'); ^^^^^^^^^^^^^ SyntaxError: Unexpec ...

Challenging Trigonometry Puzzles in Javascript

In my project, I am creating an interactive application where a group of humans and bacteria engage in a game of chase. However, I encountered a problem where instead of moving directly towards their target, all entities would veer to the left. I attempt ...

Error: The JS Exception has not been handled, as it is stating that 'undefined' is not an object when evaluating 'global.performance.now' in react-native-tvOS

I am currently working on a React-Native-tvOs app and despite following all the instructions from the react-native-tvOs GitHub page, I keep encountering an error when running the default template app. Even after running the script provided on the GitHub re ...

Using jQuery to send data with an AJAX post request when submitting a form with

This is the code I'm working with: <html> <body> <?php include('header.php'); ?> <div class="page_rank"> <form name="search" id="searchForm" method="post"> <span class="my_up_text">ENTER THE WEBSITE TO ...

I am looking to retrieve products based on category alone, category and model together, or simply obtain all products in one go. At the moment, I am utilizing three distinct endpoints

I have 3 endpoints on my backend that fetch products based on certain criteria. I'm considering whether to refactor the logic and combine them into a single endpoint/function. Currently, my routes are structured as follows: router.get('/products& ...

Implement the anti-flickering script for Google Optimize in a NextJS/ReactJS environment

While working on a NextJS/ReactJS project, I am experimenting with setting up Google Optimize for some tests. One issue I have encountered is the flickering effect that occurs when Optimize changes visual elements during experiments. To address this probl ...

Error in JavaScript: A surprise anonymous System.register call occurred

Within Visual Studio 2015, there exists a TypeScript project featuring two distinct TypeScript files: foo.ts export class Foo { bar(): string { return "hello"; } } app.ts /// <reference path="foo.ts"/> import {Foo} from './f ...

Exploring the ID search feature in JavaScript

I'm facing an issue with implementing a search feature in my project. Here is the HTML snippet: HTML: <input type="text" id="search"> <video src="smth.mp4" id="firstvid"> <video src="smth2.m ...

Should I specify each protected route in the middleware file in the matcher for NextJs 14?

Below is the middleware file I've implemented: import { NextResponse } from "next/server"; import { NextRequest } from "next/server"; import { getApiAuth } from "./app/middleware/api/auth"; const validateApi = (req: Requ ...

What steps do I need to take to modify the text in the ionic navbar

Here is a snippet of my code... <ion-side-menu-content> <ion-nav-bar class="bar-stable"> <ion-nav-back-button> </ion-nav-back-button> <ion-nav-buttons side="left"> <button class="button b ...

Encountering issues when passing a string as query parameters

How can I successfully pass a string value along with navigation from one component to another using query parameters? Component1: stringData = "Hello"; this.router.navigate(['component2'], { queryParams: stringData }); Component2: ...

Using Typescript and webpack to detect variables that are defined in the browser but not in Node environment

My goal is to create a package that can be used on both servers and clients with minimal modifications required. Some libraries are available in Node but not in a browser, while others are accessible in a browser but not in Node. For instance, when utili ...

jQuery automatic slider for seamless transitions

I am in need of assistance with the coding for a website I'm currently constructing at www.diveintodesign.co.uk/ChrisMcCrone/index.html The coding being used is sourced from http://jquery.malsup.com/cycle/ The central large image functions as a slid ...

React and Redux encounter an issue where selecting a Select option only works on the second attempt, not the first

I am currently working on a React/Redux CRUD form that can be found here. ISSUE RESOLVED: I have encountered an issue where the state should be updated by Redux after making an API call instead of using this.setState. The concept is simple: when a user s ...

Experiencing a lack of information in express?

Whenever I attempt to send a POST request (using fetch) with the body containing the state of the application, I receive an empty object on the server side. What am I doing wrong here? I should be receiving the object with the properties name, username, an ...

Form submission requires a checkbox to be checked

I have been searching for a way to make checkboxes required. Is there a method similar to using required="required"? Currently, I generate my checkboxes in the following manner and would really appreciate any guidance on this topic. It's crucial for m ...