Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
jQuery contents from a higher level?

I have the following jQuery that mostly works:

$("article > p, article > div, article > ol > li, article > ul > li").contents().each(function() {
    if (this.nodeType === 3) {
        strippedValue = $.trim($(this).text());
        doStuff(strippedValue);
    }

    if (this.nodeType === 1) {
        strippedValue = $.trim($(this).html());
        doStuff(strippedValue);
    }
})

The problems comes when (inside doStuff()) I try to replace HTML tags. Here is a view of my elements:

enter image description here

And I'm trying to replace those <kbd> tags thusly:

newStr = newStr.replace(/<kbd>/g, " <b>");  
newStr = newStr.replace(/<\/kbd>/g, "<b> ");

That doesn't work, and I'm seeing in the debugger that the <kbd> tags are seen as first-class children and looped separately. Whereas I want everything inside my selectors to be seen as a raw string so I can replace things. And I realize I'm asking for a contradiction, because .contents() means get children and their contents. So if I have a selector that is a direct parent of <kbd>, then <kdb> ceases to become a raw string and becomes instead a node that is being looped.

So it seems like my selectors are wrong BUT whenever I try to bring my selectors higher in the hierarchy, immediately I lose textual contents and I end up with a bunch of html with no contents inside the elements. (The screenshot shows good contents, as expected.)

So for example I tried this:

$("article").contents().each(function() {
   ...
}

...hoping that the selector looping would occur a little higher, and thus allow HTML tags further down to come through as raw text. But clearly I'm lost.

My objective is to simply perform a bunch of string replacements on the contents of the html. But there are two challenges with this:

  1. The page contents load dynamically, with ajaxy calls or similar, so full contents are not available until about a second or two after page load.
  2. When I try to grab high-level elements such as body, it ends up devoid of much of the textual contents. The selectors I currently have don't suffer from that problem; those get everything I want BUT then HTML/XML elements get looped instead of coming through as plain text so that I can perform replacements.
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Why do you need to perform the modification on raw HTML? You could just replace the DOM elements directly (not to mention that this is much more reliable then using string replacement):

$('kbd').replaceWith(function() {
  return ` <b>${this.textContent}</b> `;
  // or directly create DOM elements:
  // const b = document.createElement('b');
  // b.textContent = this.textContent;
  // return b;
});
console.log($('b').length);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<kbd>hello world</kbd>

Of course you can still do string replacements where it makes sense, but you should work with DOM elements as much as possible.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!