Get text from div without child elements

Related searches

I'm trying to parse this html with the help of goquery. I can't figure out how to parse the string "The string I need" while throwing away everything else.

  <div class="outter-class">
        <h1 class="inner-class">
        The string I need
        <span class="other-class" >Some value I don't need</span>
        <span class="other-class2" title="sometitle"></span>
        <div class="other-class3">
            <h3>Some heading i don't need</h3>

I tried to use something like: by adapting it to goquery like that:

test := s.Clone().Children().Empty().End().Text()

But that doesn't work. I tried a lot of different variants from the API but I can't figure it out.

The way I got it to work was with:

// End() lets us jump back to the h1 selection to get its text
text := doc.Find("h1").Children().Remove().End().Text()
text = strings.TrimSpace(text)


The string I need

The code removes the child nodes (the span elements) from the h1 element to get the correct output. There may be an easier way of doing this, but it works. :)

How to Get HTML Elements' Text Excluding Children Using jQuery , Ways to retrieve an element's text whilst ignoring text from nested child tags. How to Get HTML Elements' Text Excluding Children Using jQuery and way of getting text without children's text from multiple elements (as well� We clone the element that you want text of, then remove all the child elements from this cloned element and then returns the text of element. I have written a small jQuery plugin “justtext” which you can use to get the text of element and ignore all child elements.

Best way I've found to accomplish this:

text := dom.Find(".inner-class").Nodes[0].FirstChild.Data

I've spent quite a bit of time using the HTML parsing library under goquery so this doesn't really seem hacky to me, but it might to some.

JavaScript one-liner to get element's text content without its child , The function will be called for each child node (including the text nodes, not only elements separated with HTML tags) that our parent element has� The elements in the collection are sorted as they appear in the source code and can be accessed by index numbers. The index starts at 0. Tip: You can use the length property of the HTMLCollection object to determine the number of child elements, then you can loop through all children and extract the info you want.

How about:

doc.Find(".outter-class .inner-class").Children().First().Text()

jQuery: Get the Text of Element without Child Element, While writing code in jQuery, I often use . text() method to get the content of any element from DOM. This function is straight forward, it strips off all the html element from a selected element and returns the remaining text. Definition and Usage. The firstChild property returns the first child node of the specified node, as a Node object. The difference between this property and firstElementChild, is that firstChild returns the first child node as an element node, a text node or a comment node (depending on which one's first), while firstElementChild returns the first child node as an element node (ignores text

HTML DOM innerText Property, Get the inner text of an element: If you set the innerText property, any child nodes are removed and replaced by a single Text node containing the specified � Find Element By Link Text; Find Element By Partial Link Text; Find Element By Tag Name; 1) Find Element By ID. Always the first choice. In order to get the ID of your element, you just have to right click on your element and click on the Inspect option. The structure of your element will be highlighted in the console:

How can I get text node without child?, Hi I have a problem - I cannot get text for element without child text nodes. Html example: <div>Text <div>Another Text</div> </div> I found element and called� Centering things is one of the most difficult aspects of CSS. The methods themselves usually aren't difficult to understand. Instead, it's more due to the fact that there are so many ways to center things. The method you use can vary depending on the HTML element you're trying to center, or whether you're centering it horizontally or vertically. In this tutorial, we'll go over how to center

Update 2018. Since this is a pretty popular answer I decided to update and beautify it a little by adding the textnode selector to jQuery as a plugin.

  • I'm afraid I can't help with 'goquery'. I'm just curious why you would use it? It's own documentation says that it's an attempted knockoff of jQuery. Why not just use jQuery.
  • I'm writing a parser for a website in Go as an exercise.
  • You'll have to change Children to Children() but then it works. Thanks!
  • Ahh yes thanks for catching the typo. I created a gist for this question so anyone in the future can have a runnable example to follow:
  • In there I also saved the initial selection from doc.Find("h1"). It reduces some computation due not having to traverse the tree again to Find("h1").
  • @alexmullins : please edit your answer with your corrections
  • Just tested this and this just returns "Some value I don't need"
  • Please add some explanation of why this code helps the OP. This will help provide an answer future viewers can learn from. See How to Answer for more information.