ScriptContext Class

C#, doesn’t support global methods, so jQuery’s $ function can’t be used as simply in Saltarelle as it is in Javascript.

A simple expression like $('#SomeElementId) in Javascript corresponds to Saltarelle C# code jQuery.Select("#SomeElementId").

As a workaround, ScriptContext class can be used:

  1. public class ScriptContext
  2. {
  3. [InlineCode("$({p})")]
  4. protected static jQueryObject J(object p);
  5. [InlineCode("$({p}, {context})")]
  6. protected static jQueryObject J(object p, object context);
  7. }

As $ is not a valid method name in C#, J is chosen instead. In subclasses of ScriptContext, jQuery.Select() function can be called briefly as J().

  1. public class SampleClass : ScriptContext
  2. {
  3. public void SomeMethod()
  4. {
  5. J("#SomeElementId").AddClass("abc");
  6. }
  7. }