General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Deleting an element
-
C4 October 23, 2017 at 10:51 pm
Deleting an element
October 23, 2017 at 10:51 pmParticipants 2Replies 3Last Activity 5 years, 3 months agoHi,
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
mag3737 October 23, 2017 at 11:27 pm
Reply to: Deleting an element
October 23, 2017 at 11:27 pmI 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.
mag3737 October 24, 2017 at 12:01 am
Reply to: Deleting an element
October 24, 2017 at 12:01 amOk, 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] -
AuthorPosts
- You must be logged in to reply to this topic.