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

Guide on uploading images to a NodeJS server from an Angular 2+ application

I have a NodeJS rest-API that can handle both images and text content in the same function. Currently, I am using multer in my NodeJS server to manage image uploads along with text content. Below is an example code snippet showcasing how I am handling this ...

Ways to stop your Browser from Caching

Developing a Facebook app has been my recent project. One thing that really bothers me is the random occurrences where changes I make to my CSS style sheet or when adding a new Javascript function do not reflect in the browser. This can be very frustrating ...

Adding an image to a server through PHP in the TinyMCE editor

Currently, I am in the process of utilizing TinyMCE text editor for uploading images using PHP and JS database. However, I find myself perplexed when it comes to sending the image to the server. Below is the snippet of the JS code being used: <script ...

React Hooks: Issue with UseRef not detecting events from Material UI Select component

I'm currently utilizing React Hooks in my project. I am attempting to trigger an onclick event using the useRef hook. const component1: React.FC<Props> = props { const node =useRef<HTMLDivElement>(null); const ClickListe ...

Incorporating a separate PHP file which includes JavaScript code

I've been struggling to make this code work. In my footer file, I have the following: footer.php: <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url: '', type: 'GET', dataType: "script", success: ...

Retrieve the id within the parentheses for each checkbox that is checked and re-check each time a checkbox is selected using Kendo UI

Working with the tricky kendo-ui has made adding selectors quite challenging; however, my current task involves simply obtaining the inner contents of the id selector upon clicking an input checkbox element. Specifically, I need to extract the text between ...

using selenium to interact with elements within the window object

I'm a newcomer to selenium and javascript and looking to incorporate the VisualEvent javascript into pages opened in a selenium-controlled browser. My goal is to access its variables from selenium in java. I've successfully completed the first ph ...

I'm experiencing an issue with fullCalendar where the dayRender function is not functioning as expected

I have been using fullCalendar and I am looking to customize the color of specific days. I have successfully created an overlay that is displayed when a user clicks on a particular day. Everything works as expected with the overlay, but now I am encounte ...

Having trouble downloading a PDF file on a local server with React and the anchor tag element

Having trouble downloading a pdf file from my react app to my Desktop. I've reached out for help with the details How to download pdf file with React. Received an answer, but still struggling to implement it. If anyone new could provide insight, that ...

What is the best way to activate Cropper once a file has been selected, without encountering a 404 error on the image?

I've been trying to integrate an image cropper into my website, but I'm encountering some issues that I can't seem to resolve. Expected outcome : Upon selecting a picture from the disk using the file input, a modal should appear prompting t ...

Exploring the potential of utilizing the "wait" function in Selenium WebDriver for

I want to automate a test on my website using the Selenium WebDriver for JavaScript. How can I approach running tests here with content that may not be ready when the page loads, such as data coming from an external API? In my case, the content is loaded ...

ASP not recognizing AngularJS input states

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="Staffing_Tool.Login" %> <!DOCTYPE html> <html> <head runat="server"> <script src="scripts/angular.min.js" type="text/javascript"></scr ...

The Vue component mistakenly saves the image to the incorrect location when the file @change event is triggered

I've encountered an issue with my image slider component. Users have the option to add an extra image to the end of the slider, but when there are multiple instances of the same slider component on a page, it always adds the image to the first compone ...

sending data between pages in a Next.js application

I'm working on a project that consists of two pages: test1 and test2. I want to pass a prop from page 1 to page 2 without using the useRouter hook or setting it as a query string. In my test1 page, I have a color variable defined as const with a value ...

Angular2 recursive template navigation

Is it possible to create a recursive template in Angular 2 without using ng-include? It's puzzling why this feature seems to be missing in Angular 2... HTML: <div ng-app="app" ng-controller='AppCtrl'> <script type="text/ng-templat ...

Implement a feature in JavaScript that highlights the current menu item

I'm currently developing a website at and have implemented a JavaScript feature to highlight the current menu item with an arrow. The issue I'm facing is that when users scroll through the page using the scrollbar instead of clicking on the men ...

Using JavaScript to control the state of a button: enable or

In the process of creating a basic HTML application to collect customer information and store it in a database, I have encountered a specific user interface challenge. Once the user logs into their profile, they should see three buttons. Button 1 = Print ...

Angular routes can cause object attributes to become undefined

I am new to Angular and struggling with an issue. Despite reading numerous similar questions and answers related to AJAX, async programming, my problem remains unsolved. When I click on the 'Details' button to view product details, the routing wo ...

Concentrate on implementing Cross-domain Ajax functionality in Opera browser

For a unique and interesting browsing experience, try using Opera 9.62 to explore the nuances of cross-sub-domain JavaScript calls with Ajax. To understand this better, follow these instructions by placing three simple files on appropriate domains. foo.ht ...

Combining state and props to efficiently pass them as props - what's the best method?

I am attempting to create a component that merges props and state together, allowing the actual view to simply focus on retrieving data from props. In the snippet of code provided below, I am experimenting with utilizing the spread operator (potentially ...