7

In my javascript code I need to get the definition of an element, but without its content - neither text nor children.

E.g. for:

<div id="my_example_id" class="ShinyClass">
   My important text
   <span> Here</span>
</div>

I would like to write a javascript fucntion that when provided with the element of the div above will return the following as string:

<div id="my_example_id" class="ShinyClass">

I have been trying with different manipulations over the elements, functions like innerHTML, outerHTML and similar, but I was unable to figure out how to fetch only the part I am interested in. Is substring until the first > the best possible solution?

EDIT: selecting the element is not part of the question - I know how to do that, no prob. Rather the question is: when I have already selected a particular element how to parse as string only its own definition.

6
  • 3
    Add the code that you've tried. Commented Jun 10, 2016 at 15:30
  • @Tushar Thanks for the constructive feedback - I added some more thoughts, not code per se, just because I am not sure how to start on that Commented Jun 10, 2016 at 15:31
  • 1
    stackoverflow.com/questions/1763479/… Commented Jun 10, 2016 at 15:33
  • @janje nope - this will also return all child contents Commented Jun 10, 2016 at 15:35
  • set innerHTML of the clone to an empty string. Commented Jun 10, 2016 at 15:42

4 Answers 4

8

UPDATE:

const div = document.getElementById('my_example_id'); // get the node
const html = div.outerHTML.replace(div.innerHTML || '', ''); // simple set logic

console.log(html);

Just some way to do this, not saying the best.

const div = document.getElementById('my_example_id');
const copy = div.cloneNode(true);
const parent = document.createElement('div');
copy.innerHTML = '';
parent.appendChild(copy); // I forgot to add this line.
const html = parent.innerHTML;

console.log(html);

Basically you create a copy of the div, create a parent, then remove innerHTML of the copied node to leave out just the 'div' itself. Append the copied node to the new parent and show the parent's innerHTML which is just the 'div' you wanted.

Sign up to request clarification or add additional context in comments.

4 Comments

In what you write what is the relation between parent and copy?
I missed 1 line, updated my answer.
Updated my answer with 2-liner, I cannot think of any better way. No node cloning, no new parent, one simple replace.
Thanks for the effort - you are already upvoted for them. Still I prefer a bit the solutions concentrating on '>' as they will be a bit more effective, espectially for heavy DOMs I think
3

you don't need to do all that fancy stuff copying it to a parent..

// make a copy of the element
var clone = document.getElementById('my_example_id').cloneNode(true);
// empty all the contents of the copy
clone.innerHTML = "";
// get the outer html of the copy
var definition = clone.outerHTML;

console.log(definition);

I threw it in a function in this fiddle: https://jsfiddle.net/vtgx3790/1/

4 Comments

i believe this would not modify the dom, right?
@BorisStrandjev - correct.
@Azamantes - thank you, good catch
If you use .cloneNode(false) or just .cloneNode() (the default is false), you'll get a clone of the element without any children. You don't have to manually empty the innerHTML then.
2

I guess that a Regex is what you need. Check if this works for you

function getHtml(selector) {
  var element = document.querySelector(selector)
  var htmlText = element.outerHTML
  var start  = htmlText.search(/</)
  var end  = htmlText.search(/>/)
  return htmlText.substr(start, end + 1)
}
alert(getHtml('.ShinyClass'))

example here

2 Comments

thanks for the suggestion - for the moment, after seeing the others' suggestions I think this is more optimal
I'm glad it helps :)
1
console.log(getElementTag("my_example_id"));

function getElementTag(myElementId) {
  var FullEelementObject = document.getElementById(myElementId);
  var FullElementText = FullEelementObject.outerHTML;
  var regExTag = new RegExp(/(<).*(>)/i);

  openingTag = FullElementText.match(regExTag);

  return openingTag[0];
}

Just threw together this JSFiddle, it gets the outerHTML of the element you pass the function, the regExp to get the full opening tag.

Edit: Here is the JSFiddle

1 Comment

No need to add a comment on the question saying you added an answer... we can see the answer. Plus, the OP gets notified of answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.