Get-UDElement is not working

I am getting null all the time for Get-UDElement. Not sure what i am doing wrong.
i am using version 2.9.0

$HomePage = New-UDPage -Name “Home” -Icon home -Content {
New-UDTextbox -Id ‘message’ -Placeholder “Enter the some text”
New-UDButton -Id “btnSearch” -Text “Click me!” -OnClick {
$txtMessage = Get-UDElement -Id ‘message’
if ($txtMessage -eq null){Show-UDToast -Message "Component is null"} #Show-UDToast -Message ($txtMessage.Attributes[‘value’])
}
}

Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Port 2000 -Content {
New-UDDashboard -Title “Welcome” -Pages @($HomePage)
}

Hi @premgoks, welcome.
See if this works for you.

$HomePage = New-UDPage -Name "Home" -Icon Home -Content {
    New-UDTextbox -Id 'message' -Placeholder "Enter some text"
    New-UDButton -Id 'btnSearch' -Text "Click Me!" -OnClick (
        New-UDEndpoint -Endpoint {
            $txtMessage = (Get-UDElement -Id 'message').attributes.value
            If($txtMessage -eq $null) {
               Show-UDToast -Message "Component is null"
            } else {
                Show-UDToast -Message $txtMessage
            }
        }
    )
}

Main things I changed, the -OnClick parameter, I put a New-UDEndpoint inside that, which let’s you run script logic. I’d recommend this article by @psDevUK, for some better info on endpoints (https://psdevuk.github.io/ud-flix/Content-vs-Endpoint/)
Set the $txtMessage variable to be the value of the Get-UDElement, since even if the field is empty or null, Get-UDelement will return a powershell object, so it technically will always have something in it.
Hope that helps.

Hi @RamonMA,
Thanks for your response, i tried your code, but still i am getting "Component is null " :frowning:

I think the “onclick” event is an endpoint in it’s own right as it runs the code inside the script block each time you click the button so I’m not sure if you need an endpoint within it.

Does this do what you’re looking for?

$HomePage = New-UDPage -Name "Home" -Icon home -Content {
    New-UDTextbox -Id "message" -Placeholder "Enter the some text"
    New-UDButton -Id "btnSearch" -Text "Click me!" -OnClick {
        $txtMessage=(Get-UDElement -Id "message").Attributes['value']
        if ([string]::IsNullOrEmpty($txtMessage)) {
            Show-UDToast -Message "Component is null"
        }
        else {
            Show-UDToast -Message ($txtMessage)
        }
    }
}
Get-UDDashboard | Stop-UDDashboard
Start-UDDashboard -Port 2000 -Content {
New-UDDashboard -Title “Welcome” -Pages @($HomePage)
}

Hi Tom,

Thank you for your time.
But still its not working . Thing is 'Get-UDElement -Id “message” ’ is always returning null.

Hm that is odd! have you tried re-installing the UD module? If I use the same code I get this: