Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:36:39+00:00 2026-06-05T22:36:39+00:00

I’m processing some 3rd party HTML which is semi-structured marked-up text (bold, italics, etc).

  • 0

I’m processing some 3rd party HTML which is semi-structured marked-up text (bold, italics, etc).

Here’s a simplified sample of the structure:

<div>
  <strong class="term">one</strong>
  -
  <b class="defs">
    foo        
    <i class="pos">verb</i>
    bar
    <i class="pos">noun</i>
    baz
    <i class="pos">adjective</i>
    blah
  </b>
  <br>
  <strong class="term">two</strong>
  ... etc ...
</div>

In fact I’ve already processed it a bit to get it into this shape. I can handle the HTML elements OK, but I haven’t been able to figure out how to deal with the interleaved text and <i> elements.

I’m happy with a solution that either splits the “defs” around the <i>s, a solution which iterates through the parts, etc. I would prefer not to mix jQuery and “raw” DOM API calls due to browser-specific quirks, but I understand if I can’t avoid it. It seems from my shallow knowledge that jQuery doesn’t have as good support for marked-up text as for “structural” HTML …

Am I missing something obvious? This seems very hard to search for…


It turns out that in the real world data, the text runs and <i> nodes are always interleaved, but the first thing within the defs may be either one, and each text run can consist of one or more actual text nodes. This means that <i>s and text runs are not in matched pairs.

Good solutions might be to either add markup to each text run, or to iterate through, doing one thing for each <i> and another thing for each text run. I’m thinking jQuery.contents() with some node type checking must be the key…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:36:41+00:00Added an answer on June 5, 2026 at 10:36 pm

    You can do the following to retrieve all the texts into an array

    $(function(){
      var json = $('.defs').contents().map(function(){
         var text = $.trim($(this).text()); 
         return  text != "" ? text : null;
      });
    
      console.log(json);
    });​
    

    Demo: http://jsfiddle.net/joycse06/Z5AgL/

    The above code Gives you a list of all the defs along with the textnode and i.

    Update

    Yeah you can do node type or name check using this.nodeName or this.nodeType inside the map function. nodeType for textnode is 3. e.g. add this inside .map() and check

    console.log(this.nodeName,this.nodeType);
    

    So for this specific markup structure you can do the following to check if it’s <i> or textnode

    if(this.nodeName.toLowerCase() == 'i'){
        console.log('<i>');
        // do stuff for <i> here
    }    
    else{
        console.log('Text Node');            
        // it's text node
    }  
    
    // or with nodeType
    
    if(this.nodeType == 3){
     // textNode
    }else{
     // <i> 
    }  
    

    Demo : http://jsfiddle.net/joycse06/Z5AgL/6/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.