18

I would like to retrieve a certain tag element with its attributes from the DOM. For example, from

<a href="#" class="class">
  link text
</a>

I want to get <a href="#" class="class">, optionally with a closing </a>, either as a string or some other object. In my opinion, this would be similar to retrieving the .outerHTML without the .innerHTML.

Finally, I need this to wrap some other elements via jQuery. I tried

var elem = $('#some-element').get(0);
$('#some-other-element').wrap(elem);

but .get() returns the DOM element including its content. Also

var elem = $('#some-element').get(0);
$('#some-other-element').wrap(elem.tagName).parent().attr(elem.attributes);

fails as elem.attributes returns a NamedNodeMap which does not work with jQuery's attr() and I was not able to convert it. Admitted that the above examples are not very senseful as they copy also the element's no-longer-unique ID. But is there any easy way? Thanks alot.

Richard Kiefer
  • 1,814
  • 2
  • 23
  • 42

9 Answers9

19

There is a way to do this without jQuery. This also works with <br> tags, <meta> tags, and other empty tags:

tag = elem.innerHTML ? elem.outerHTML.slice(0,elem.outerHTML.indexOf(elem.innerHTML)) : elem.outerHTML;

Because innerHTML would be empty in self-closing tags, and indexOf('') always returns 0, the above modification checks for the presence of innerHTML first.

gilad905
  • 2,842
  • 2
  • 16
  • 23
Aaron Gillion
  • 2,227
  • 3
  • 19
  • 31
  • 1
    This doesn't work with empty elements: document.createElement("strong").outerHTML.slice(0, elem.outerHTML.indexOf(elem.innerHTML)) – Martin Jun 01 '17 at 11:19
  • @Martin put your `createElement` declaration in a variable called `elem` and use the ternary expression I used above, it returns ``. – Aaron Gillion Jun 01 '17 at 22:04
  • 1
    @AaronGillion Good catch. That one could be remedied by searching for `><` if innerHTML is an empty string. This makes me think of another issue, though: ` ` identifies the wrong index for the space. – MDMower Oct 04 '17 at 21:03
  • 4
    ***This is very UNSAFE***. E. g. when **`elem.outerHTML === '

    p

    '`**. Or any other situation that innerHTML's text is a subset of tagName or one of its attributes or even attributes' values!
    – Mir-Ismaili Apr 25 '19 at 21:18
  • as another comment said: very **Unsafe** -- if **multiple** other text has the **same** content of innerHtml... otherwise (if you dont care that, and dont care empty tags), wouldnt `split()` be a lot easier?... – Nor.Z Feb 04 '23 at 19:33
7
var wrapper = $('.class').clone().attr('id','').empty();
  • You might want to change the selector to more exactly match the <a> element you're looking for.
  • clone() creates a new copy of the matched element(s), optionally copying event handlers too.
  • I used attr to clear the element's ID so that we don't duplicate IDs.
  • empty() removes all child nodes (the 'innerHTML').
josh3736
  • 139,160
  • 33
  • 216
  • 263
  • Amazing. Thanks very much, josh :) Any concerns about using `.clone()`? Some say it may not be that performant, but I cannot see any other solution. – Richard Kiefer Mar 07 '12 at 15:37
  • i guess .clone() isn'e exactly copy the whole object and some sort of reference do exists and if we use .remove() to the cloned object, it actually removes the original object. I suffer this issue once. – Nadeem Yasin Mar 07 '12 at 15:38
  • @RichardKiefer: Performance is obviously browser-dependent, but this code [ranges from 3,000 ops/sec in IE8 to 25,000 ops/sec in FF10](http://jsperf.com/jq-clone-perf). You probably shouldn't be generating thousands of DOM elements anyway, so I don't see anything to worry about. – josh3736 Mar 07 '12 at 15:56
3

Here is my solution:

opentag=elem.outerHTML.slice(0, elem.outerHTML.length-elem.innerHTML.length-elem.tagName.length-3);

I suppose, that close tag is of the form: "</"+elem.tagName+">".

Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
sus
  • 31
  • 2
3

Unfortunately, @AaronGillion's answer isn't reliable as I said in my comment. Thank @sus. I recommend his/her way with a little change to support <self-closing tags />:

function getOpenTag(element: HTMLElement): string {
    const outerHtml = element.outerHTML;
    const len = outerHtml.length;
    
    const openTagLength = outerHtml[len - 2] === '/' ? // Is self-closing tag?
            len :
            len - element.innerHTML.length - (element.tagName.length + 3);
    // As @sus said, (element.tagName.length + 3) is the length of closing tag. It's always `</${tagName}>`. Correct?
    
    return outerHtml.slice(0, openTagLength);
}

The code is in TypeScript. Remove types (HTMLElement and number) if you want JavaScript.

Sam Dutton
  • 14,775
  • 6
  • 54
  • 64
Mir-Ismaili
  • 13,974
  • 8
  • 82
  • 100
3

If someone is not using jQuery . . .

elem.outerHTML
"<a href="#" class="class">
  link text
</a>"
elem.cloneNode().outerHTML
"<a href="#" class="class"></a>"

If you want to be safe for Firefox etc. released before mid-2014, use cloneNode(false) to avoid getting inner stuff.

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode

Marcus
  • 3,459
  • 1
  • 26
  • 25
1

Here's a solution I've used:

const wrap = document.createElement('div')
wrap.appendChild(target.cloneNode(true))
const openingTag = wrap.innerHTML.split('>')[0] + '>'
0

This can be done without any String manipulation.

Instead, you can use the element's internals and build the string yourself from there:

function getTagHTML(el) {
  if (!el instanceof HTMLElement) return null;
  let result = `<${el.tagName.toLowerCase()}`;
  for (const attribute in el.attributes) {
    if (el.attributes[attribute].nodeValue) 
      result += ` ${el.attributes[attribute].name}="${el.attributes[attribute].nodeValue.replace(/"/g, "&quot;")}"`
  }
  result += `></${el.tagName.toLowerCase()}>`;
  return result;
}

console.log(getTagHTML(document.getElementById('outer')));
<div id="outer" class='i-want-"this"'>
  <span>I do not want this</span>
</div>

Please note that for self-closing elements like <img /> this would give you an unwanted and incorrect closing tag. Feel free to adjust the code accordingly.

connexo
  • 53,704
  • 14
  • 91
  • 128
0

+1 for cloneNode answer above

  • create HTML without content
  • Looping over attributes
  • optional end tag
  • optional self-closing tag

function getTagHTML(el, includeClosingTag=true , selfClosing = false) {
  //return el.cloneNode(false).outerHTML;
  if (el.attributes)
    return `<${el.localName}` + 
      [...el.attributes].map(
             attr => ` ${attr.name}="${attr.nodeValue.replace(/"/g, "&quot;")}"`
      ).join`` +
      `${selfClosing ? "/" : ""}>` + 
      (!selfClosing && includeClosingTag ? `</${el.localName}>` : "");
  else 
    return null;
}

document.body.querySelectorAll("*:not(script)").forEach(el=>{
  console.log(getTagHTML(el));
  console.log(getTagHTML(el,false));
  console.log(getTagHTML(el,false,true))
});
<div id="ONE" class='CLASSNAMES' attr1='"string"'>
   INNERHTML
  </div>
  <img id="TWO" src="https://svgshare.com/i/Uhq.svg"/>
Danny '365CSI' Engelman
  • 16,526
  • 2
  • 32
  • 49
0

My solution using Reg Exp to replace all possible closing tags inside element attributes:

const outerHTML = div.outerHTML;
const content = outerHTML.slice(0, outerHTML.length - div.innerHTML.length);
const dummy = content.replace(/'.*?'|".*?"/g, x => 'x'.repeat(x.length));
return content.slice(0, 1 + dummy.indexOf('>'));
Uahnbu Tran
  • 109
  • 9