Modifying the CSS style of an element on PageA by clicking a button on PageB

For my app, I am incorporating tabs. I want to implement a user button that, when clicked on tab-detail.html, will update the CSS of an element located on its parent tab page, tab.html.

.controller('TabCtrl', function($scope,Tabs) {
    $scope.tabs = Tabs.all() ;
    // This fills in the "tab.html" template
    // An element on this page is: <span id="tab_selected_1">
    // When a user selects a listed item on tab.html
    // It redirects to tab-detail.html
})

.controller('TabDetailCtrl', function($scope,$stateparams,Tabs) {
   $scope.tabs = Tabs.get($stateparams.tabID) ;
    // On tab-detail.html, there is a button <button ng-click="tabSelect()">
   $scope.tabSelect = function(thisID) {
      // Update the css on TabCtrl's elementID
      document.getElementById('tab_selected_1').style.color = "green" ;
   }
})

The only way to access tab-detail.html is through tab.html, therefore tab.html must be loaded. However, no matter what approach I take, I can't seem to find a way to reach the element that belongs to another controller's page.

I have attempted:

var e = angular.element('tab_selected_1');

or

var e = angular.element(document.querySelector('tab_selected_1') ;
e.style.color = "green" ;

Answer №1

Your current method may not be effective since the desired DOM is unavailable. Consider creating a shareable service to store the necessary variables and use them in your UI. To ensure proper binding, structure your service variable as an object like styleData, or utilize an angular constant for this purpose.

app.constant('constants', {
   data: {
   }
});

You can then inject this constant into your controller and make modifications as needed.

.controller('TabCtrl', function($scope, Tabs, constants) {
    $scope.constants = constants; //access constants in HTML
    $scope.tabs = Tabs.all();
    // code for populating "tab.html" template and handling user selections
})

.controller('TabDetailCtrl', function($scope,$stateparams,Tabs, constants) {
   $scope.tabs = Tabs.get($stateparams.tabID);
   $scope.constants= constants; //access constants in HTML
    // code for handling tab detail and updating CSS
   $scope.tabSelect = function(thisID) {
      $scope.constants.data.color = "green";
   }
})

Markup

<div id="tab_selected_1" ng-style="{color: constants.data.color || 'black'}">

Answer №2

Here's a suggestion for achieving this task: 1) Start by creating a new service 2) Assign a value to a variable within the service when a button is clicked (within tab-detail.html) 3) Utilize the value of that service variable in tab.html

Answer №3

(Update: Correction below)

The solution provided by @pankajparkar does work, however it seems to be incompatible with IONIC due to the framework's override of DOM settings. Even though the style is being added inline and defined in the DOM Element inspector, the color of the rendered HTML remains unchanged.

Research suggests that this issue is specific to IONIC, as other users have encountered similar problems. While there may be workarounds suggested on various forums and blogs, none have proven successful thus far.

To assist others in the future (even if not compatible with IONIC), the code has been reformatted, but a solution for IONIC compatibility is still sought:

.constant('constants', {
   tabColors: {
     curID:0,
   },
}) 

.controller('TabCtrl', function($scope,Tabs,constants) {
  $scope.constants = constants; 
}

.controller('TabDetailCtrl', function($scope,$stateparams,Tabs,constants) {
  $scope.constants = constants; 
  $scope.setItem= function(thisID) {
    $scope.constants.tabColors.oldID = $scope.constants.tabColors.curID ;
    delete $scope.constants.tabColors['tabID_'+$scope.constants.tabColors.curID] ;
    $scope.constants.tabColors.curID = thisID ;
    $scope.constants.tabColors['tabID_'+thisID] = 'green' ;
}

// HTML in Tab.html
<span id='tab_tabID_{{tab.tabID}}' ng-style="{color: constants.tabColors['tabID_'+tab.tabID] || 'black'}">
Some Text Here
</span>

//HTML in TabDetail.html
<button id="tab_button" class="button button-small button-outline button-positive" ng-click="setItem({{tab.tabID}});">
   Select This Item
</button> 

Correction: The method does work and is compatible with IONIC. The problem lies in IONIC elements inheriting properties from predefined classes within tags like <ion-item> and <ion-nav>. To address this, either update the class or include ID tags on each element and apply CSS changes as shown above. Although not ideal, this approach will yield results.

In my scenario, the HTML structure was as follows:

<span id='tab_tabID_{{tab.tabID}}' ng-style="{color: constants.tabColors['tabID_'+tab.tabID] || 'black'}">
  <h2>Header Text Here</h>
  <p>More text here</p>
</span>

The mentioned CSS method works well with this layout:

<span id='tab_tabID_{{tab.tabID}}'>
  <h2  ng-style="{color: constants.tabColors['tabID_'+tab.tabID] || 'black'}">
    Header Text Here
  </h>
  <p  ng-style="{color: constants.tabColors['tabID_'+tab.tabID] || 'black'}">
    More text here
  </p>
</span>

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

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

Cannot access mobile navigation due to expanded browser search bar

Greetings, all! Currently, as I am in the process of developing a website for my company, I have encountered an issue with the navigation menu, specifically on mobile devices, or more accurately, mobile browsers(most of them). The hamburger icon seems to ...

Hover over buttons for a Netflix-style effect

https://i.stack.imgur.com/7SxaL.gif Is there a way to create a hover effect similar to the one shown in the gif? Currently, I am able to zoom it on hover but how can I also incorporate buttons and other details when hovering over it? This is what I have ...

Can two controllers be used for the main-app and sub-app with the same URL using angular ui-route?

I have implemented two controllers, 'BaseController' and 'SubController', for my main application and sub-application. Both controllers are configured to point to an empty URL. Below is the configuration for the main application:- ang ...

Sending a request from AngularJS to a Django API to retrieve a JWT token results in a 405 error response

After making a POST request to obtain the token in an endpoint that is supposed to accept POST requests, I keep receiving a 405 error. I am working with rest_framework and rest_framework_jwt Here is the code for the endpoint: from django.conf.urls impor ...

Issue encountered while retrieving ImageURI using Phonegap

Currently, I am working on a feature that allows users to choose a photo from their camera or gallery using PhoneGap 3.0.0 with Angular integration. However, I have encountered an issue where it only seems to work for photos taken with the camera. Below ...

What is preventing me from applying styles to the first word in my Angular ngFor iteration?

I'm currently attempting to customize the initial word of a string within my Angular ngFor loop. Strangely, the class gets applied but the style defined in my CSS file is not. Inline styling also does not seem to work - any ideas why? This is the CSS ...

Directive's ng-click function fails to execute

This directive contains a function that should be executed when clicked. ebApp.directive('monthDir', function () { return { restrict: 'E', templateUrl: 'htmlFiles/monthDirective.html', transclu ...

The background appears only in the upper portion of the screen upon refreshing the table

Whenever the user clicks on the edit button or when the page is initially loading, a backdrop appears and disappears once the modal is displayed. The backdrop effectively highlights the entire page. However, after updating my table with data via Ajax witho ...

Problems Arising from Bootstrap Label and Input Field Alignment

I'm encountering an issue with my Angular app that uses Bootstrap 4. I'm trying to have my form label and input text box appear on the same line, but instead, they are displaying on separate lines. I've tried searching for a solution, but ha ...

Utilize CSS to exclusively filter through items

Is it possible to use CSS alone to filter a list of items based on specific links being clicked? I have a list that should display only certain items when corresponding links are clicked. HTML: <!-- Header links --> <a href="#" class="all" tabin ...

Tips on aligning a div to the right of a headline

My goal is to have an orange circle image placed to the right of my headline <h2>. However, I am facing an issue where the circle jumps above the headline when I shrink the screen. How can I ensure that it stays on the right side no matter the screen ...

Having trouble displaying toasts on my website using Angular Material design and $mdToast

Hello there, I am very new to AngularJS, and I have just started using the basic function Show() from $mdToast. Below is the complete code that I have written for this issue, and I would greatly appreciate any help you can provide. 'use strict&apos ...

What is the correct way to display or hide a button based on my specific situation?

Creating a 'show more' button for my app has presented me with a challenge. I am using a directive to handle actions when the user clicks on the read more button: (function(window, angular) { var app = angular.module('myApp'); ...

Interactive pop-up messages created with CSS and JavaScript that appear and fade based on the URL query string

I have a referral form on this page that I want people to use repeatedly. After submitting the form, it reloads the page with the query string ?referralsent=true so users can refer more people through the form. However, I also want to show users a confir ...

Tips for aligning a div underneath another div in a centered position

I am attempting to align .rij_lessenrooster below .rij_weekdagen so that it is centered under each day. The teacher advised me to create a column and then try it, but it did not resolve the alignment issue. The .rij_weekdagen consistently appears off to th ...

What is the best way to create an arrow connecting a parent div to multiple child divs?

I'm faced with a challenge involving a reusable React list component that supports nested children. My goal is to visually connect the parent div to its direct children using arrows, similar to the image linked below. View List Component Image Below ...

Tips for inserting manual line breaks in justified text

My task involves taking a 5-page story from a Word document and implementing it on a website. #reading { hyphens: auto; text-align: justify } <div style="font-size: 20px; width: 750px; margin-bottom: 4em;" class="reading" id="reading"> TextT ...

The vanishing act: Semantic UI menu disappears when you click

My objective is to create a persistent left-side menu using Semantic-UI. I want to implement two different states for the menu - one with text for each item and another with an image for each item. However, I am facing some challenges that have me complete ...

magnify within a div with a set width

Looking for a way to make a div both zoomable and scrollable within a fixed-width container using transform: scale(aScaleRatio) for zooming in/out. Check out this demo for achieving the zoom effect. However, the inner div seems to have trouble aligning to ...