Set-UDElement not working (with -Attributes @{style=@{display='none'}} )

Product: PowerShell Universal
Version: 2.10.1

I’m trying to toggle the visibility of a UDElement using set-UDElement.

In this minimal code example I have two script blocks that can be attached to the onClick event of the button in order to hide/show the UDElement with id=‘item-content’.

toggleHidden_1 uses Set-UDElement and doesn’t work

toggleHidden_2 is a workaround using Invoke-Javascript that does work but is obvioulsy not the preferred solution.


    [scriptblock] $toggleHidden_1 = {
                        if((get-udelement -id "item-content").attributes.style.display -eq "none"){
                            set-udelement -id "item-content" -Attributes @{style=@{display="block"}}
                        }else{
                            set-udelement -id "item-content" -Attributes @{style=@{display="none"}}
                        }
        }

    [scriptblock] $toggleHidden_2 = {
                        Invoke-UDJavaScript -JavaScript (
                            @"
            var elem = document.getElementById("item-content");
            if (elem.style.display === "none"){
                elem.style.display = "block";
            }else{
                elem.style.display = "none";
            }
"@)}

    # replace $toggleHidden_1 with $toggleHidden_2 and the example will work
    New-UDButton -Text "show/hide" -OnClick $toggleHidden_1

    New-UDElement `
        -tag div `
        -id "item-content" `
        -Attributes @{style = @{display='none'}} `
        -Content {
            "Click the button to hide/show this content"
        }
}

Any ideas on what I’m doing wrong? Help is much appreciated!

This certainly seems like a bug but I got it to work like this:

    [scriptblock] $toggleHidden_1 = {
        Show-UDToast (Get-UDElement -Id "item-content").attributes.style.display        

        if ((Get-UDElement -Id "item-content").attributes.style.display -eq "none") {
            Set-UDElement -Id "item-content" -Attributes @{ 
                    attributes = @{ 
                        style = @{ 
                            display = "block" 
                        } 
                    } 
                }
        }
        else {
            Set-UDElement -Id "item-content" -Attributes @{ 
                    attributes = @{ 
                        style = @{ 
                            display = "none" 
                        } 
                    } 
                }
        }
    }
1 Like

Thank you, Adam.
Your workaround does the trick.
This is much better than the Invoke-UDJavascript workaround.