-1

With jquery, I need to get <details> content, but without <summary> part. Is there any way to do it without regex to remove summary content?

It's for a tinymce plugin.

The content may be in a single tag like :

<details>
  <summary>blablabla</summary>
  <div>
    ... / ...
  </div>
</details>

or with multiple childre like this:

<details>
  <summary>blablabla</summary>
    <p>
      ... / ...
    </p>
    <p>
      ... / ...
    </p>
    <p>
      ... / ...
    </p>
</details>

Here is what I have:

var nodeType = 'details';
var summary = '';
var details = '';

var selectionNode = tinymce.DOM.getParent(editor.selection.getNode(), nodeType);
if( ( selectionNode !== null ) && selectionNode.nodeName.toLowerCase() === nodeType ){
    summary = // here I must get summary content
    details = // here I must get details content, without summary part
}

thanks if you have any clue to help me ;-)

1 Answers1

-1

$(document).ready(function() {
  let summary = $("details summary").replaceWith("");
  let details = $("details");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<details>
  <summary>blablabla</summary>
  <p>
    ... / ...
  </p>
  <p>
    ... / ...
  </p>
  <p>
    A... / ...A
  </p>
</details>

$("details summary").replaceWith("");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<details>
  <summary>blablabla</summary>
  <p>
    ... / ...
  </p>
  <p>
    ... / ...
  </p>
  <p>
    A... / ...A
  </p>
</details>

Use replaceWith method of jQuery as shown below:

$("details summary").replaceWith("");

Refer: https://api.jquery.com/replacewith/

K K
  • 17,794
  • 4
  • 30
  • 39
  • OP doesn't want to **change** the DOM. They just need the textual content separately. – connexo Apr 14 '21 at 08:16
  • @connexo where did the OP mention that? – K K Apr 14 '21 at 08:18
  • Right in their introduction: *With jquery, I need to get
    content, but without part.* and also in the example code comments.
    – connexo Apr 14 '21 at 08:18
  • @connexo So does it imply that he don't want to change the DOM? Or is it just your interpretation. Let the OP decide it for himself what he needs and what not. After the above operation, he can directly get the contents using the simple $("details") selector. – K K Apr 14 '21 at 08:19
  • Ultimately any answer on SO is an interpretation of the question asked. Here it is quite clear though, that OP is not looking for what you are suggesting. – connexo Apr 14 '21 at 08:22
  • @connexo That's exactly what I mentioned, *quite clearly*. Let OP decide if this answer is helpful for him or not. Let him downvote or upvote according to his needs. – K K Apr 14 '21 at 08:24