General XMetaL Discussion

  • C4

    Deleting an element

    Participants 2
    Replies 3
    Last Activity 4 years, 11 months ago

    Hi,

    I am using XMetaL Author Essential 11

    I am trying to delete an element and it is a child of another element. I am using this code but when it finds the last instance of this element, it deletes the parent element as well. How do I stop this from happening?

    var rng=ActiveDocument.Range;
    rng.MoveToElement(“autovalue”,true);
    rng.Select();
    if (Selection.CanRemoveContainerTags) {
    Selection.RemoveContainerTags();
    }
    ]]>

    Thanks

    Reply

    mag3737

    Reply to: Deleting an element

    I think we might need a little more context here. For example:

    – What is the parent of the autovalue element?
    – Is there any other markup structure involved here?
    – Where might the user's cursor/highlight be located when this operation is performed?

    I tried out your code to remove the tags from elements within paragraphs (changing “autovalue” to “b”), and the code worked fine if the cursor was placed anywhere before the first and then executed repeatedly. Each execution deleted the next set of tags it found. The

    was never removed.

    Reply

    mag3737

    Reply to: Deleting an element

    Ok, I think the answer is that before you do the RemoveContainerTags() you need to verify that after doing MoveToElement() your range/selection is actually located inside the element whose tags you want to remove. Otherwise, if it so happens that the tags of the resulting location can also be validly removed, they will be.

    Try this:

    [code]
    var rng=ActiveDocument.Range;
    rng.MoveToElement(“autovalue”, true);
    rng.Select();
    if ((Selection.CanRemoveContainerTags) && (Selection.ContainerName == “autovalue”)) {
    Selection.RemoveContainerTags();
    }
    [/code]

    As a side note, you might consider doing the rng.Select() after the tags are removed, instead of before. This should work just as well, with the bonus(?) effect that the cursor will not move unless some tags actually do get removed (i.e. if the code performs a no-op, the user's cursor will remain where it was):

    [code]
    var rng=ActiveDocument.Range;
    rng.MoveToElement(“autovalue”, true);
    if ((rng.CanRemoveContainerTags) && (rng.ContainerName == “autovalue”)) {
    rng.RemoveContainerTags();
    rng.Select();
    }
    [/code]

    Reply

    C4

    Reply to: Deleting an element

    Thanks – that works.

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support