cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Change the fill color of an element with Javascript

Change the fill color of an element with Javascript

iamt_ps
New Poster

Hello,

i want to ask if there is a way to change the fill color of an iq-shape Element with the setProperty Command in Javascript. I have tried it with this code but unfortunately it doesn't work:


        const cancelable = shmi.onReady( {
            controls: {
                Kreis: ".Kreis"
            }
        }, function (resolved) {
            resolved.controls.Kreis.shape-svg.setProperty("fill", "#ff0000", "important");
        } );
 
I know, that I could use a Stylesheet to do this but in my project I need to change the color of multiple elements frequently and I don't want to create Stylesheets for all of them if there is a way to do it like this.
 
Thanks for Helping
6 REPLIES 6

webiq-sk
Frequent Contributor

It looks like you assumed something which you probably didn't validate. Your assumptions were:
1. onReady gives you HTML elements
2. the Shape widget is just a pure SVG

Both is not true - onReady returns the widget instance, because otherwise you could not interact with scripts with the widget. You have to access the element property to get the HTML element. The widget HTML is not the SVG, because WebIQ has some standards for how widgets are built in HTML:

webiqsk_0-1716467272815.png

It's always recommended to use the browser development tools (F12) to see the structure of the HTML element.

Here is some working code:

        //Place your Code here
        const cancelable = shmi.onReady( {
            controls: {
                circle: ".my-shape"
            }
        }, function (resolved) {
            const svg = resolved.controls.circle.element.getElementsByTagName('svg')[0];
            svg.style.fill = '#ff0000';
        } );

        /* called when this local-script is disabled */
        self.onDisable = function () {
            self.run = false; /* from original .onDisable function of LocalScript control */
            cancelable.cancel();
        };

I have tried your example but it doesn't work for me. I know, that the script is executed because there are other functions that work fine but these lines seem to do nothing:

const svg = resolved.controls.circle.element.getElementsByTagName('svg')[0];
svg.style.fill = '#ff0000';

What can I do to get this to work?

webiq-sk
Frequent Contributor

Sorry, I can't help you without you providing the HMI that has the issue. I tested it before and it worked in my HMI. Please note that my element is called "circle" and not Kreis, so you cannot simply copy & paste it and it will work.

Unfortunately I am not allowed to provide the full project but I have created a test project, where I copied the element and the script with the color change. I added a command to move the element a bit to the right side, to see if the script is executed, but the command to change the color still does nothing. Thanks for helping!

webiq-sk
Frequent Contributor

Thank you, it cannot work the way you did, because you used instance styling which always overrides any custom styling:

webiqsk_0-1716809971627.png

It always must override any other styling because it cannot "know" what the user wants (there can only be one winner). If you remove this, then it works as intended.
I also recommend to familiarize yourself with the browser's developer tools as this makes it easy to find out why it doesn't work:

webiqsk_1-1716810237164.png

There is no difference here between writing JavaScript in WebIQ or any other website - it's exactly the same here - this explains CSS specificity: https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

The information on the instance styling was omitted from the original description so I couldn't provide a sufficient explanation - in the future please always provide all information available - that will make it a lot easier.

You can however override this without removing the instance styling in WebIQ Designer, but this can only be done by setProperty:

//svg.style.fill = '#ff0000';
svg.style.setProperty('fill', '#ff0000', 'important');
 
Then this will work, because of the "important" added.
Also, there's no need to use long objects paths like resolved.controls.circle.element as I already assigned this to the svg variable.
 

 

Alright thank you very much it now works for me. Also thanks for the other tipps since it is my first Web HMI project I appreciate all informations that may help in this project.

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist