JSEvent Method

Official Content
This documentation is valid for:

Allow using the controls’ “Click” and “OnChange” events to trigger Java Script code.

Syntax

Control.JSEvent('Event_JavaScript', "Your_JavaScript_code")

Where:

Event_JavaScript’
    Is the name of the Java Script event to be dealt

"Your_JavaScript_code"
    code to be dealt.

Description

The valid values are:  "onclick" (Edit, Button, Pictures, Bitmap type variables and Text Blocks type controls) and "onchange" (for Combo, Dynamic Combo, Listbox and Dynamic Listbox type controls).

It must be possible to include the Java Script code written in the second parameter in an IF; i.e.: it must return TRUE or FALSE.
If the result is TRUE, the control associated event will be triggered. 

Example

ButtonName.JSEvent( 'onclick', "confirm( 'Are you sure?')")

When the user presses the button, the confirmation will appear and, only if it answers Yes, the GeneXus event associated to the button will be executed (if any). 

Scope

Objects: Transactions, Web Panels
Controls: Bitmap Type Variables, Buttons, Combos, Dynamic Combos, Dynamic List Boxes, List Boxes, Pictures, Text Blocks
Languages: .NET, Java, Ruby (up to GeneXus X Evolution 3)

Compatibility

Warning: Since GeneXus 15 it is recommended to use whenever possible External Objects for Javascript instead of the JSEvent event. The JSevent use cases are still valid to trigger a function based on user confirmation (such as the javascript confirm function).

Since GeneXus 16 upgrade 5 it is neccesary to modify Javascript code for complex conditionals. If you have conditional similar to:

ButtonControlName.JSEvent("onclick", !"if (/*conditions*/) {confirm('Something');} else {true;}")

you need yo change it in the following way adding an anonymous wrapper function and make sure to always return a True or False value:

ButtonControlName.JSEvent("onclick", !"function() {if (/*conditions*/) { return confirm('Something');} else { return true;} }()")

otherwise the following error will appear on the javascript console

SyntaxError: Unexpected token 'if'
    at new Function (<anonymous>)

See Also

Selecting Events