UDCheckBox value reference - how?

I need to retrieve the value of a checkbox. All the literature point to using the Attributes property, but… it doesn’t exist.

New-UDCheckbox -Id ‘chkbox’ -OnChange {
Show-UDToast -Message (Get-UDElement -Id ‘chkbox’).Attributes[‘checked’]
}

That results in a “Cannot index into a null array” error. The above isn’t what I’m trying to achieve, just an example of Attributes not returning anything.

The other thing I’ve tried is:

($session:chkContactSearchLocked = New-UDCheckbox -Id ‘chkbox’)
New-UDButton -Text “Search” -OnClick {
Show-UDToast -Message ($session:chkContactSearchLocked.checked)
}

but still no joy - either as a session var or otherwise.

I feel like I’m missing something here, if someone can enlighten me!

Yo @fromtheinternet hopefully I can enlighten you…so depending which version you are using (im on 2.5.2) if you just do start-uddashboard -port 8011 then visit that page @adam has done a great job on adding more documentation and examples and code for UI Components…One of the demos is:-

New-UDCheckbox -Label “Checkbox” -OnChange {
Show-UDToast -Message $EventData
}
But to also further this example here is one from a dashboard I am using:-
New-UDInput -Id “option1” -Title “Keep Or Remove” -Content {
New-UDInputField -Type ‘textbox’ -Name ‘Vehicle_ID1a’ -Placeholder ‘Enter the vehicle ID’
New-UDInputField -Type ‘radioButtons’ -Name ‘RemoveKeep’ -Placeholder @(“Keep 1”, “, Remove 2”) -Values @(“1”, “2”)
} -Endpoint {
param($Vehicle_ID1a, $RemoveKeep)
$statQuery2 = @"
UPDATE FLEET.dbo.Vehicle
SET Active_ID = $RemoveKeep
WHERE Vehicle_ID = $Vehicle_ID1a
"@
Invoke-Sqlcmd2 -ServerInstance YOUR_SERVER -Database FLEET -Query $statQuery2 -Username YOUR_USERNAME -Password YOUR_PASSWORD
New-UDInputAction -Toast “Vehicle ID $Vehicle_ID1a has now been updated.” -Duration 3000
}

Hopefully this will bring a smile to your face :smile:

Doh…sorry the other post i did was on radio buttons not check boxes, I do apologise didn’t get much sleep…

1 Like

@fromtheinternet Specifically what you’re looking for is the $EventData variable. It’s auto-generated inside the -OnChange script block. It’s simply a Boolean that is true if the checkbox is ticked, and false if the checkbox is unticked. Cheers!

1 Like

Excellent, thanks both. I now have a working solution!

1 Like