javascript - Change all Elements (html) of a div on changing the language? -


i have dropdown menu on page has language selector options. on selecting language want labels , buttons html changed according language? my code

var arr = [];     //  gets ids starting comp_     $('div[id^="comp_"]').each(function(){         arr.push(this.id);         labels = $(this).find('label');         buttons = $(this).find('button');          //get labels inside current div         $(labels,buttons).each(function(){             $(this).html("");         });      });     console.log(arr); }, 

*problem * changes label element reference , not button reference.can run function on multiple element references?

it works if dont want repeat same code again diffferent references

    var arr = [];     //  gets ids starting comp_     $('div[id^="comp_"]').each(function(){         arr.push(this.id);         labels = $(this).find('label');         buttons = $(this).find('button');          //get labels inside current div         $(labels).each(function(){             $(this).html("");         });          $(buttons).each(function(){             $(this).html("");         });      });     console.log(arr); }, 

yes:

    labels.add(buttons).each(function(){         $(this).html("");     }); 

or just:

    labels.add(buttons).html(''); 

one character shorter:

    labels.add(buttons).empty(); 

the .add() jquery method used add elements existing jquery collection. examples use .add() combine elements in "labels" , "buttons" jquery objects. second 2 indicate don't need .each() if you're doing invariable each element. jquery functions intrinsically operate on every element of collection.

totally different way simplify:

    var labelsandbuttons = $(this).find('label, button');     labelsandbuttons.empty(); 

the , in selector string "or". example finds elements tag name "label" or "button".


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -