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 569037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:15:07+00:00 2026-05-13T13:15:07+00:00

peace folks, I have this small piece of jquery based code, that grabs all

  • 0

peace folks, I have this small piece of jquery based code, that grabs all tags that has class “foo” and their childs and assign them some value, my question how could i get this working in the same exact way but not using jquery, but native javascript

jQuery.each(data.results, function(index, value) {
      jQuery(".foo").find("*")
        .andSelf()
           .contents()
           .filter(function(){
               return this.nodeType === 3;
           })
           .filter(function(){
               // Only match when contains 'simple string' anywhere in the text
         if (value.origin != ""){
                      return this.nodeValue === (value.origin);
          }
           })
           .each(function(){
              this.nodeValue = "assign me";
           });});
  • 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-05-13T13:15:08+00:00Added an answer on May 13, 2026 at 1:15 pm

    It’ hard to see what exactly you are trying to do here, but here are a couple of functions that might help you and an example of their use (as I understand your requirement)

    function getTextNodes( el ) {
        var nodes = [];
    
        if( el.length ) { //perhaps a better test for an array/collection of objects is required here?
            for( var i = 0, j = el.length; i < j; i++ ) {
                //call this function with each item in the array/collection
                nodes = nodes.concat( arguments.callee( el[i] ) );
            }
            return nodes;
        }
    
        for( var i = 0, j = el.childNodes.length; i < j; i++ ) {
            var node = el.childNodes[i];
            if( node.nodeType == 3 ) {
                //ignore whitespace
                if( /^\s+$/.test( node.nodeValue ) ) continue;
                nodes.push( node );
            } else {
                //call this function with this child node
                nodes = nodes.concat( arguments.callee( node ) );
            }
        }
        return nodes;
    };
    
    
    function getByClassName( className ) {
        //some browsers already have this function
        if( document.getElementsByClassName ) {
            return document.getElementsByClassName( className );
        }
        var els = [];
        var candidates = document.getElementsByTagName( '*' );
        for( var i = 0, j = candidates.length; i < j; i++ ) {
            if( candidates[i].className.indexOf( className ) > -1 ) {
                els.push( candidates[i] );
            }
        }
        return els;
    };
    
    
    function trim( str ) {
        return str.replace( /^\s+/, '' ).replace( /\s+$/, '' );
    };
    

    USE:

    //grab all the textnodes in elements with class foo
    var textNodes = getTextNodes( getByClassName( 'foo' ) );
    
    //loop through the data performing your replacement
    for( var key in data ) {
        if( !data[key].origin ) continue;
        for( var i = 0, j = textNodes.length; i < j; i++ ) {
            if( trim( data[key].origin ) == trim( textNodes[i].nodeValue ) ) {
                textNodes[i].nodeValue = 'assign me';
            }
        }
    }
    

    A couple of points on your original

    • It’s inefficient – for each loop of your data you find the textnodes in foo elements all over again, you only need to find them once
    • Chaining in jQuery does allow very succinct code but doesn’t make it understandable. I think the point above is as a result of wanting to chain everything

    better jQuery

    var textnodes = jQuery( ".foo" )
        .find( "*" )
        .andSelf()
        .contents()
        .filter( function() {
            return this.nodeType === 3;
         });
    
    jQuery.each( data.results, function( index, value ) {
        textnodes.filter( function() {
            // Only match when contains 'simple string' anywhere in the text
            if( value.origin != "" ) {
                return this.nodeValue === (value.origin);
            }
        })
        .each(function(){
            this.nodeValue = "assign me";
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a weirdest thing, in this peace of code a browser adds tags
I have this peace of code which brings string searches for img tags in
If I have this peace of code: <rich:select enableManualInput=true defaultLabel=Select time spent> <f:selectItems value=
I have a peace of html that looks like this: <h3>PETIT DEJEUNER</h3> <p>FRANS ONTBIJT</p>
Guys I have this peace of code I'm using phonegap with jqueryMobile to develop
I have this peace of code $doc = new DOMDocument(); $doc->loadHTML($article_header); $imgs = $doc->getElementsByTagName('img');
I have follwowing peace of code in which I have specified the leaky line
Hey folks, ok so this is my problem, I need to display HTML code
I have the following peace of code: <h:selectOneMenu id=countrycode styleClass=Width300 value=#{customer.countrycode} valueChangeListener=#{customer.countrycodechange} onchange=submit() immediate=true
I've got problem with this peace of code, it should change lower case letters

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.