1

I have the following in HTML code:

<div id="arM">
<metryczka>[ DGW RP ] - Gazeta Wyborcza nr
<numergazety>280</numergazety>, wydanie <mutacja></mutacja>
z dnia 02/12/2013<dzial>Ukraina się burzy</dzial>, str.
<strona>2</strona>
</metryczka>
</div>

To retrieve the contents of <metryczka> I use:

document.getElementById("arM").getElementsByTagName("metryczka")[0].InnerText

And I get:

[ DGW RP ] - Gazeta Wyborcza nr
<numergazety>280</numergazety>, wydanie <mutacja></mutacja>
z dnia 02/12/2013<dzial>Ukraina się burzy</dzial>, str.
<strona>2</strona>

But how do I get e.g. everything between </mutacja> and <dzial> (that is z dnia 02/12/2013) or [ DGW RP ] - Gazeta Wyborcza nr? I have heard of document.querySelector but I don't know if it is applicable here.

menteith
  • 596
  • 14
  • 51
  • you are looking for text nodes jQuery: http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery no jQuery: http://stackoverflow.com/questions/10730309/find-all-text-nodes-in-html-page – or hor Apr 22 '16 at 09:47

1 Answers1

1

You can get the textNode by using nextSibling, previousSibling or childNodes based on the tag

For example :

console.log(
  document.getElementById("arM").getElementsByTagName("mutacja")[0].nextSibling.textContent, //  using nextSibling
  document.getElementById("arM").getElementsByTagName('metryczka')[0].childNodes[0].textContent, // using childNodes
  document.getElementById("arM").getElementsByTagName('numergazety')[0].previousSibling.textContent // using previousSibling
)
<div id="arM">
  <metryczka>[ DGW RP ] - Gazeta Wyborcza nr
    <numergazety>280</numergazety>, wydanie
    <mutacja></mutacja>
    z dnia 02/12/2013
    <dzial>Ukraina się burzy</dzial>, str.
    <strona>2</strona>
  </metryczka>
</div>
Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • This is not the expected output – Rajshekar Reddy Apr 22 '16 at 09:51
  • @Reddy : `how do I get e.g. everything between and (that is z dnia 02/12/2013) or [ DGW RP ] - Gazeta Wyborcza nr` – Pranav C Balan Apr 22 '16 at 09:54
  • Yes thats right, But your output is not as mentioned ... you have a extra `280, wydanie ` and also `Ukraina się burzy, str. 2` – Rajshekar Reddy Apr 22 '16 at 09:57
  • @Reddy : check the console... for me it's shows `z dnia 02/12/2013 [ DGW RP ] - Gazeta Wyborcza nr` – Pranav C Balan Apr 22 '16 at 09:58
  • yes I thought the stuff on screen was the output, I added `http://gh-canon.github.io/stack-snippet-console/console.min.js` to show console output on the snippet.. – Rajshekar Reddy Apr 22 '16 at 10:02
  • 1
    Upvoted and accepted. Much appreciated. This answer gives the expected result. In `AutoHotKey` (a language used to automate Windows tasks) all of the snippets work but one – `childNodes[0].textContent`. I think `childNodes` fails to work in this language. It doesn't change the fact the answer is perfect. Many thanks. – menteith Apr 22 '16 at 10:17
  • @menteith : glad to help you :) – Pranav C Balan Apr 22 '16 at 10:18