Javascript .innerHTML but excluding inner div
Asked Answered
D

6

6

Considering I have this div:

<div class="ResCheckIn">
  <div class="ResDtlLabel">
    Check-in
   </div>
   Thursday, October 18, 2018
</div>

I simply want to get the string "Thursday, October 18, 2018", so excluding the inner div. If I try with .innerHMTML:

document.querySelectorAll('.ResCheckIn').innerHTML;

It returns everything in .ResCheckIn, including the inner div with class "ResDtlLabel", so this:

<div class="ResDtlLabel tSmall tLight">
    Check-in</div>
Thursday, October 11, 2018

And ideally I would love to do that without using jQuery.

Diggs answered 19/9, 2018 at 11:12 Comment(1)
you only want the textnodes?Velarde
G
6

One option is iterating through childNodes and filtering the textNodes. The following function gets all the textNode children and stores their values in an array.

const nodes = document.querySelector('.ResCheckIn').childNodes;
const texts = [].reduce.call(nodes, function(ret, el) {
  if ( el.nodeType === 3 ) {
    ret.push(el.nodeValue.trim());
  }
  return ret;
}, []);

texts is an array of textNodes contents.

Greene answered 19/9, 2018 at 11:21 Comment(2)
you might want to use Node.TEXT_NODE instead of 3 for better readability.Velarde
Or directly el.nodeName === "#text"Fagaceous
E
2

Not a very innovative answer but if you are looking for simple solution try this

var content = document.querySelector(".ResCheckIn").innerHTML;
    content = content.split('</div>');
    if(content[1]){
        content = content[1].trim();
    }
Erratic answered 19/9, 2018 at 12:22 Comment(0)
F
1

You can use like this:

document.querySelectorAll('.ResCheckIn')[0].textContent
Fadge answered 19/9, 2018 at 11:27 Comment(1)
This will return all the textSivas
R
1

Using jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    var html=$('.ResCheckIn').html();
    var parsedText=$.parseHTML(html);
     $.each(parsedText, (i, el) => {
          if (el.nodeType === 3) {
          if(el.nodeValue.trim()!=""){              
             console.log(el.nodeValue);
             alert(el.nodeValue);
           }
          }
     });
  });
</script>
<div class="ResCheckIn">
  <div class="ResDtlLabel">
    Check-in
   </div>
   Thursday, October 18, 2018
</div>
Rosebay answered 19/9, 2018 at 11:37 Comment(0)
D
1

You have to use "content of node". for better understanding about this please look into this link.

There are multiple ways you can achieve your result. below is one of way,

$(document).ready(function() {
  var parentnode = document.querySelectorAll('.ResCheckIn')[0];
  console.log($(parentnode).contents().filter(function() {
    return this.nodeType == 3;
  }).text().trim());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="ResCheckIn">
  <div class="ResDtlLabel">
    Check-in
  </div>
  Thursday, October 18, 2018
</div>
Dragonnade answered 19/9, 2018 at 11:53 Comment(0)
G
-1
// Example container.
let container = document.getElementsByClassName('ResCheckIn')[0];

// Loop over child elements and remove them. Note that this is ES6 syntax and may not yet be supported in all browsers.
Array.from(container.children).forEach(em => { 
    em.parentElement.removeChild(em);
});

console.log(container.textContent);

Just a simple example. If you remove only all child elements (and not nodes) from anHTMLElement (like container in this example), the text nodes are skipped and you are left with the text only.

Gisarme answered 19/9, 2018 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.