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!