Skip to content Skip to sidebar Skip to footer

Accessing Dynamic Property Names In Jquery/javascript

In a plugin I'm writing, the dev can specify options, which I'm storing and referencing like so: (function( $, window) { $.widget('mobile.plug', $.mobile.widget, { opti

Solution 1:

You should be able to concatenate "Width" to the "panel" result:

o[el.jqmData("panel") + "Width"]

E.g., if el.jqmData("panel") is "menu" you would get o["menuWidth"].

Solution 2:

What you're looking for is bracketed notation with strings:

for (var i = 0; i<elems.length; i++){
   var el = elems.eq(i);
   console.log( o[el.jqmData("panel") + "Width"] );
   }

...assuming that el.jqmData("panel") returns "mid", "menu", etc.

In JavaScript, you can refer to a property with either dotted notation and a literal (obj.foo), or bracketed notation and a string (obj["foo"]). In the latter case, it doesn't have to be a literal string, it can be the result of an expression.

Post a Comment for "Accessing Dynamic Property Names In Jquery/javascript"