CRecordWebAddFirstChild: Insert a Child Element at the Beginning of an HTML Element

since version 2.5

The message CRecordWebAddFirstChild is used to insert a new child element at the beginning of the list of child elements of an existing HTML element (the parent element) in the DOM structure.

This feature is part of the RemoteSkin Web API for DOM manipulation and has been available since version 2.5. The associated logic is implemented within the plugin NY_RemoteSkinWebProtocolAPI.

Message Structure (JSON)

The message is identified under the name WEB_ADD_FIRST_CHILD. It requires two mandatory slots to define the target and the content to be inserted:

{
  "id": "c8123725-6e09-4a8b-a03f-71d5eb13effb",
"name": "WEB_ADD_FIRST_CHILD",
  "description": "Add an element as first child.",
  "slots": [
    {
      "key": "1",
      "name": "ID",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The ID of the target or parent element."
    },
    {
      "key": "2",
      "name": "HTML",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The HTML code of the element to be inserted."
    }
  ]
}

Arguments

Argument Type Mandatory Description
ID STRING Yes The ID of the HTML element that serves as the parent element and to which the new child element will be added.
HTML STRING Yes The HTML code of the child element to be inserted at the beginning.

Usage with the Helper Class CWebApi

Using the CWebApi helper class is the recommended and easiest way to invoke this functionality. The method encapsulates the creation of the message.

This example inserts the list item <li>Banana</li> as the first element into a list with the ID id_of_my_list:

mWebApi.addFirstChild("id_of_my_list",
                      "<li>Banana</li>");

Usage as a Message

The functionality can also be used directly by creating and sending the CRecordWebAddFirstChild record. The target address (CTargetAddress) of the RemoteSkin target is required for this.

public void addFirstChild(@NotNull final CTargetAddress aRemoteSkinAddress,
                          @NotNull final String aIdOfParentElement,
                          @NotNull final String aChildElement) throws CException
{
    // 1. Create the Envelope (CEnvelope) for the RemoteSkin target
    final CEnvelope env = CEnvelope.forSingleTarget(aRemoteSkinAddress);
    
    // 2. Create the CRecord object
    final CRecord record = CRecordWebAddFirstChild.create();
    
    // 3. Set the mandatory ID and HTML arguments
    CRecordWebAddFirstChild.setId(record,
                                  aIdOfParentElement);    
    CRecordWebAddFirstChild.setHtml(record,
                                  aChildElement);

    // 4. Send the message as a Notification
    sendNotification(env,
                     record);
}

See also