0
const locale = {
    fr: {
        login_page_title_welcome: "a",

        BKUE_status: "b",
        UNCN_status: "c",
        NNTA_status: "d",
    }
};

var l = locale["fr"];

$.fn.locale=function(i) {

    this.html(l.i);
    this.attr('locale', ""+i+"");
};

I have the const and I want to update an elements html and add an attribute using a custom method.

login_div_UNCN.locale('UNCN_status');

The attribute is added by the method. But the html is not updated.

Am I doing something wrong?

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • You left out how `login_div_UNCN` is created. – Taplar Jan 03 '19 at 19:00
  • Also `locale.fr` does not have a child property of `i` for `html(l.i);` to work. You need to use `l[i]` if `i` is the variable for the property – Taplar Jan 03 '19 at 19:01
  • What is the context of `this`? – Diodeus - James MacFarlane Jan 03 '19 at 19:03
  • @Taplar Its a const linked to an ID of a DIV. – Nikk Jan 03 '19 at 19:04
  • @Taplar If I were to use `l.login_div_UNCN` it works. Why do I need `l[login_div_UNCN]`. Don't get it? – Nikk Jan 03 '19 at 19:05
  • Ok, so then you just need to fix your incorrect usage of `l.i`. Because `l.i` expects `l` to be a variable and `i` to be the **literal** name of the property. In this case it is not. It's a variable. So to make javascript understand it is a variable, you use bracket notation, so it uses the variable as a reference for the actual property name based on its value – Taplar Jan 03 '19 at 19:05
  • @Taplar Ok, `l[i]` works as intended. Can you please explain why this works? – Nikk Jan 03 '19 at 19:06
  • Possible duplicate of [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Taplar Jan 03 '19 at 19:07

0 Answers0