DOM manipulation is one of the fundamental capabilities of the WebForms class. WebForms Core provides several method groups for modifying HTML elements, their attributes, styles, text, and form-related content.
These operations are described on the server and executed in the browser by WebFormsJS. The API separates them into different groups according to how each operation affects the existing DOM. Understanding these differences is important because methods with similar names do not necessarily have the same behavior.
2.1. Add
The Add group is used to add content or properties to an existing element. Its behavior is additive: if the target property or content does not exist, it is created; otherwise, the new value is appended to the existing value.
The available methods include:
-
Element properties:
AddId,AddName,AddValue,AddClass,AddStyle -
Form controls:
AddOptionTag,AddCheckBoxTag,AddHidden -
Text and labels:
AddTitle,AddLabel,AddText,AddTextToUp -
Attributes and elements:
AddAttribute,AddTag,AddTagToUp,AddTagBefore,AddTagAfter
For example, the following calls add a CSS class and a style to an existing element:
webForms.AddClass("profile", "active");
webForms.AddStyle("profile", "color", "blue");
The AddText method appends text to the target element:
webForms.AddText("message", "Welcome!");
The AddTag method creates a new HTML element inside the specified target:
webForms.AddTag("container", "div", "new-item");
The group also includes methods for adding elements before or after a target, adding content at the beginning of an element, and creating hidden inputs. This provides several ways to extend an existing HTML structure without replacing its current content.
2.2. Set
The Set group is used when the intention is to assign a new value rather than append to an existing one. If the target property or content does not exist, it is created. Otherwise, its existing value is replaced.
The available methods include:
-
Element properties:
SetId,SetName,SetValue,SetClass,SetStyle -
Form controls:
SetOptionTag,SetChecked,SetCheckBoxTag,SetSelectedValue,SetSelectedIndex,SetCheckedValue,SetCheckedIndex -
Text and labels:
SetTitle,SetLabel,SetText -
Attributes:
SetAttribute -
Dimensions and appearance:
SetWidth,SetHeight,SetBackgroundColor,SetTextColor,SetFontName,SetFontSize,SetFontBold,SetTextAlign -
Element behavior:
SetVisible,SetReadOnly,SetDisabled,SetFocus,SetMinLength,SetMaxLength,SetCustomValidity
For example, SetText replaces the existing text content of an element:
webForms.SetText("message", "Your changes have been saved.");
Similarly, SetClass assigns a new class value:
webForms.SetClass("profile", "active");
The group also provides convenient methods for common styling operations. For example, dimensions can be specified using either strings or integers:
webForms.SetWidth("panel", 500);
webForms.SetHeight("panel", "300px");
webForms.SetFontSize("message", 18);
When an integer is supplied for width, height, or font size, the class automatically adds the px unit.
The Set group is particularly useful when updating the current state of an element, such as changing form values, replacing text, adjusting visibility, or modifying its appearance.
2.3. Insert
The Insert group has a different behavior from both Add and Set. It creates the specified data only if it does not already exist. If the target property or content is already present, the operation does nothing.
The available methods include:
-
Element properties:
InsertId,InsertName,InsertValue,InsertClass,InsertStyle -
Form controls:
InsertOptionTag,InsertCheckBoxTag -
Text and labels:
InsertTitle,InsertLabel,InsertText -
Attributes:
InsertAttribute
For example:
webForms.InsertClass("profile", "active");
webForms.InsertAttribute("profile", "data-status", "ready");
These operations are useful when a property should be added only when it is missing, without replacing an existing value.
This behavior makes Insert particularly suitable for conditional initialization of element properties and attributes.
2.4. Delete
The Delete group provides methods for removing specific properties, content, or elements from the DOM.
The available methods include:
-
Element properties:
DeleteId,DeleteName,DeleteValue,DeleteClass,DeleteStyle -
Form controls:
DeleteOptionTag,DeleteAllOptionTag,DeleteCheckBoxTag,DeleteAllCheckBoxTag -
Text and labels:
DeleteTitle,DeleteLabel,DeleteText -
Attributes and elements:
DeleteAttribute,Delete,DeleteParent
For example, a CSS class can be removed without affecting other classes:
webForms.DeleteClass("profile", "active");
An individual option can also be removed from a selection control:
webForms.DeleteOptionTag("country", "DE");
To remove all options from the target selection control, use:
webForms.DeleteAllOptionTag("country");
The Delete method removes the target element itself, whereas DeleteParent targets its parent element:
webForms.Delete("notification");
webForms.DeleteParent("notification");
This distinction is useful when an application needs to remove either a specific element or the container that holds it.
2.5. Update
In addition to the four main groups, the WebForms class provides an Update group for more specialized modifications. These methods are intended for changing existing values or portions of HTML content without having to replace the entire element.
The main methods are:
IncreaseDecreaseReplaceReplaceStartTag
The class also provides more specific methods for increasing or decreasing common properties:
-
Increase:
IncreaseMinLength,IncreaseMaxLength,IncreaseFontSize,IncreaseWidth,IncreaseHeight,IncreaseValue -
Decrease:
DecreaseMinLength,DecreaseMaxLength,DecreaseFontSize,DecreaseWidth,DecreaseHeight,DecreaseValue
Incrementing and decrementing values
The Increase and Decrease methods allow a numeric value to be adjusted by a specified amount:
webForms.Increase("counter", 5);
webForms.Decrease("counter", 2);
For common properties, dedicated methods are also available:
webForms.IncreaseFontSize("message", 2);
webForms.DecreaseWidth("panel", 20);
webForms.IncreaseValue("quantity", 1);
These methods provide a concise way to express incremental changes without having to calculate and assign the resulting value manually on the server.
Replacing content
The Replace method replaces a specified value within the target element:
webForms.Replace("content", "old text", "new text");
It also supports two optional parameters: alsoStartTag and deep.
The alsoStartTag parameter determines whether the replacement should also be applied to the start tag. The deep parameter controls how nested HTML tags are handled. When deep mode is disabled, tags inside the target element are treated as strings. Deep mode, by contrast, does not remove inner elements.
For example:
webForms.Replace("content", "old", "new", false, true);
Replacing start-tag content
The ReplaceStartTag method is designed specifically for replacing content within an element's start tag:
webForms.ReplaceStartTag("profile", "data-status=\"old\"", "data-status=\"active\"");
This method can be useful when modifying start-tag attributes without using a general content replacement operation. Attribute names should be supplied in lowercase because HTML converts attribute names to lowercase.
2.6. Understanding the Differences
Although these method groups may appear similar, each one represents a different modification strategy:
| Group | Behavior |
|---|---|
Add |
Creates missing data or appends to existing data |
Set |
Creates missing data or replaces the existing value |
Insert |
Creates data only if it does not already exist |
Delete |
Removes the specified data or element |
Update |
Performs incremental changes or targeted replacements |
This separation is a useful aspect of the WebForms API. Instead of relying on one general-purpose method for every DOM operation, developers can express their intentions explicitly. The resulting commands are then processed by WebFormsJS in the browser.
Together, these groups provide the basic building blocks for server-side DOM manipulation in WebForms Core, while preserving the browser as the environment in which the actual DOM changes take place.
Top comments (0)