-1

i am translating a web pages using this javascript i generated below, i have a problem converting text in an input so i try to put if condition inside my code, if it is an input use return if not use .html, but im having problem with the return because it is not working and i dont know why (please see the comments in the code to give you more info)

function changeLanguage(lang){
$.post("/misc/sessionUpdate.asp",{ 'lang': lang}); 
$.getJSON("/language/"+lang+".json", function( data ) {

    $.each(data, function(property, val){

        $('[data-localize]').val(function() {
            type =  $(this).attr("type");
            attrVal = $(this).attr("data-localize");
                    // if put return data[property]; here it is working but the problem it should be inside the if(attrVal == property)
            if(attrVal == property){
                if (type == "submit"){
                    // i already tried console.log (attrVal +"=="+ property) to verify if there is an instance of true and there is
                    return data[property];
                }else{
                    attrName = '[data-localize="'+property+'"]'; //sample format [data-localize=menu]
                    $(attrName).html(val); // data[property] is the corresponding value of the key in json
                }
            }               
        });
    });
}).done(function(){
    $('.languageLoader').css('display','none');
});
}

i also put the algorithm in .done function to avoid assynchronus problem in ajax and still not working

thanks in advance guys....

coriano35
  • 165
  • 1
  • 3
  • 10
  • You have your variables defined?? – Krishna Prashatt Jun 26 '18 at 05:50
  • You are returning value from an anonymous function which will not be received by anyone. – Rajesh Jun 26 '18 at 05:52
  • yes it is, in fact even if i just use static (ex: return "string";) string to return it is still not working, but if i put it before the condition if(attrVal == property) it will work – coriano35 Jun 26 '18 at 05:52
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Rajesh Jun 26 '18 at 05:53
  • Is attrVal == property and type == "submit" true? If not then you won't get a return it seems. Both need to be true for return. – Nikhil Goswami Jun 26 '18 at 06:42
  • For the each record in data "$('[data-localize]').val" this will update all [data-localize] elements. If property in condition is not getting matched then anonymous function inside $('[data-localize]').val() will not return anything so automatically its value will be reset. So you final values will be the values as per the last record in data. – Karan Jun 26 '18 at 06:59

1 Answers1

1

Update your loop like below.

$.each(data, function(property, val){
    $('[data-localize="' + property + '"]').each(function(index, item) {
        type =  $(this).attr("type");   
        if (type == "submit") {
            $(this).val(data[property]);
        } else {
            $(this).html(val);
        }
    });
});
Karan
  • 12,059
  • 3
  • 24
  • 40