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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:54:32+00:00 2026-06-17T15:54:32+00:00

In a page I call external js file and its includes a dictionary. I

  • 0

In a page I call external js file and its includes a dictionary. I want to replace element text by their attiributes.

<p data-text="dict.dataP"></p>
<p data-text="dict.data.P2"></p>

I want to fill it with external file dict

var dict = {
    "dataP1": "this is my p text",
    "data" : {
         "p2": "my another paragraph"
    },

};

I tried to use as this

$.each(function () {
    if ($(this).attr("data-text").length > 0) {
        $(this).html(
        $(this).attr('data-text') /*we got a problem here. it returns string*/ );
    }
});

codePen here:
codepen.io

JSFiddle here
jsfiddle.net

How can I do this?

edited dict. we have dictionary in dictionary.

  • 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-17T15:54:33+00:00Added an answer on June 17, 2026 at 3:54 pm

    There are a few things that you should change to get this working, taking Jimbo’s answer and adding a few other things:

    var dict = {
      "dataP": "this is my p tag text"
    };
    
    $('[data-text]').each(function () {
      if ( $(this).data("text") ) {
        var dictAttr = $(this).data("text");
        if ( dictAttr && dict[dictAttr] ) {
          $(this).html( dict[dictAttr] );
        }
      }
    });
    

    You should also change your mark-up to make things easier (expecially if you are only dealing with one dict object):

    <p data-text="dataP"></p>
    

    With the above changes you should get things working as you expect.

    http://jsfiddle.net/b2zgw/

    string-based object navigation

    Rather oddly I’m actually working on a framework for string-based object navigation as we speak. However the codebase if far too complicated to just tack on the end of a stackoverflow answer. As an addition to my comment I realised there is a third option which might be the best of both worlds (depending on what you need).

    :: eval

    I don’t recommend this method, but it can’t be argued with for simplicity — the following is a snippet from the code above and should replace that part:

    var dictNav = $(this).data("text"); /// e.g. say dictNav == 'dict.dataP'
    var ref; eval('ref='+dictNav);
    if ( ref && red.substr ) {
      $(this).html( ref );
    }
    

    The problem with the above is that eval will evaluate any JavaScript – so all an attacker would have to do is find a way to add a data-text attribute to something on your page and they would be able to execute any JavaScript they liked.

    :: string-based navigation

    The following is a simple function that could be improved, but should give you the idea:

    function navigate( obj, path ){
      var cur = obj, bits = path.split('.');
      for ( var i=0, l=bits.length; i<l && cur; i++ ) {
        cur = cur[bits[i]];
      }
      return cur;
    }
    

    Using the above you can navigate your dictionary structure – be aware because you start your ‘navigation path’ with a particular var name (i.e. dict) you’ll have to place your dictionary one level down of the object you wish to traverse; this is the reason for {dict:dict}. To support multiple dictionaries you’d just need to extend that object like so {dict:dict,dict2:dict2} — again the following is a snippet and should replace the original code:

    var dictNav = $(this).data("text"); /// e.g. say dictNav == 'dict.dataP.test'
    var ref = navigate({dict:dict}, dictNav);
    if ( ref && red.substr ) {
      $(this).html( ref );
    }
    

    :: third option – simplify your dictionary

    The third option is probably the most optimal, but means that the dictionary can’t be modified in realtime (or at least not easily). Basically before you run the rest of your code, push your dictionary through a parsing function that modifies it’s structure:

    var dict = {
       "dataP": "this is a test",
       "forms": {
         "inputLabel": "this is a second level"
       }
    };
    
    function simplifyDict( obj, target, path, subpath ){
      if ( !target ) { target = {}; }
      if ( !path ) { path = ''; }
      for( var i in obj ) {
        subpath = (path ? path + '.' : '') + i;
        if ( typeof obj[i] == 'object' ) {
          simplifyDict( obj[i], target, subpath );
        }
        else {
          target[subpath] = obj[i];
        }
      }
      return target;
    }
    
    dict = simplifyDict( dict );
    

    The above simplify function should return something like:

    {
      "dataP" : "this is a test",
      "forms.inputLabel" : "this is a second level"
    }
    

    Then all you need to do is use my original code right at the top of this answer. Instead of converting your strings to separate object properties and navigating the dictionary, you’ve switched the dictionary to be a string-based lookup… so now you can just use strings directly i.e. dict['forms.inputLabel']

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

Sidebar

Related Questions

I want to call a web page from Flash and use the data returned
I want to replace the content of the #textoGrande div with an external file.
I want to call a html page on click of a button in mvc.
When I run my page sometimes the getElementById call finds the element and sometimes
I have this jquery script to call an external file. So far so good.
I'm currently using below method to call an external php file with some variables
How can I call external javascript in my default admin html page in Django.
I am working on a web page that pulls an external javascript file that
I have a page that calls an external application.js . This external application.js file
I'm trying to call a Page Method using a jQuery 'attached' event function, in

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.