/    Sign up×
Community /Pin to ProfileBookmark

Pass object in function instead of string?

In the following example, I use the function “SetUp(SBox,AObjStr)” to send an object reference to the function “Populate(SBox,SObj)”.

Seems to work OK, but it is a 2 step process to assign the JSON objects to the selected drop-down boxes.

Fairly easy, but somewhat tedious, to change to fewer or more levels as desired with a common bit of code.

However, what I would like to do is simplify it a bit more … 😮
Is there a way to bypass the “SetUp” function to “Populate” the multi-level drop downs directly? ?
I think what I’m looking for is a way to pass a string in the function that would be recognized as an object (in this case a JSON or other form of array)
and be decoded to add options to the drop-down choices,
but I don’t have a clue as to how that might be accomplished without the extra “SetUp” function!

Anyone have an idea I could try? ?

[code]
<html>
<head>
<title> Passing Object Indirectly </title>
<script type=”text/javascript”>

// var SelectLine = [‘transportation’,’type’,’model’];

var Transport = { ‘Select’:”,’Air’:’Air’,’Land’:’Land’,’Sea’:’Sea’ };

var Air = { ‘Select’:”,’Planes’:’Planes’,’Motorized Ultralight’:’Ultralight’,’Free-flight Glider’:’Glider’ };
var Planes = { ‘Select’:”,’747′:”,’737′:”,’Cessna’:” };
var Ultralight = { ‘Select’:”,’Kolb Flyer’:”,’Kitfox’:” };
var Glider = { ‘Select’:”,’Schwietzer Glider’:” };

var Land = { ‘Select’:”,’Car’:’Car’,’Truck’:’Truck’,’Train’:’Train’ };
var Car= { ‘Select’:”,’BMW Z3′:”,’Chevy Malibu’:”,’Lincoln LS’:” };
var Truck = { ‘Select’:”,’F-150′:”,’S-10′:”,’Tahoe’:” };
var Train= { ‘Selectl’:”,’Freight Train’:”,’Passenger Train’:” };

var Sea = { ‘Select type’:”,’Ship’:’Ship’,’Boat’:’Boat’,’Submarine’:’Submarine’ };
var Ship = { ‘Select’:”,’Oil Tanker’:”,’Cruise’:”,’Yacht’:” };
var Boat = { ‘Select’:”,’Cris-Craft’:”,’Ski’:”,’Sail’:”,’Oar’:” };
var Submarine = { ‘Select’:”,’Los Angeles Class’:”,’Kilo Class’:”,’Seawolf Class’:” };

function Populate(SBox,SObj) {
oSel = document.getElementById(SBox);
oSel.options.length = 0;
// oSel.options[oSel.options.length] = new Option(‘Select an option’,”,false,false);
for (var x in SObj) {
oSel.options[oSel.options.length] = new Option(x,SObj[x], false,false);
}
oSel.selectedIndex = 0;
}

/* alternate version of above
function Populate(SBox,SObj) {
oSel = document.getElementById(SBox);
oSel.options.length = 0;
var tarr = []; for (var x in SObj) { tarr.push(x); } // could tarr.sort() something!
oSel.options[oSel.options.length] = new Option(tarr[0],”,false,false);
for (var i=1; i<tarr.length; i++) {
oSel.options[oSel.options.length] = new Option(tarr[i],SObj[tarr[i]], false,false);
}
oSel.selectedIndex = 0;
}
*/

function SetUp(SBox,AObjStr) { // alert(SBox+’n’+level);
switch(AObjStr) {
case ‘Transport’: Populate(SBox,Transport); break;

case ‘Air’: Populate(SBox,Air); break;
case ‘Planes’: Populate(SBox,Planes); break;
case ‘Ultralight’: Populate(SBox,Ultralight); break;
case ‘Glider’: Populate(SBox,Glider); break;

case ‘Land’: Populate(SBox,Land); break;
case ‘Car’: Populate(SBox,Car); break;
case ‘Truck’: Populate(SBox,Truck); break;
case ‘Train’: Populate(SBox,Train); break;

case ‘Sea’: Populate(SBox,Sea); break;
case ‘Ship’: Populate(SBox,Ship); break;
case ‘Boat’: Populate(SBox,Boat); break;
case ‘Submarine’: Populate(SBox,Submarine); break;

case ”: break;
default : alert(‘Invalid entry: ‘+level); break; // should never occur with good choices
}
}

onload = function() { SetUp(‘SBox0′,’Transport’); }
</script>

</head>
<body>

<select id=”SBox0″ onchange=”SetUp(‘SBox1’,this.value)”></select>
<select id=”SBox1″ onchange=”SetUp(‘SBox2’,this.value)”></select>
<select id=”SBox2″></select>

</body>
</html>
<!–
/* Could also be 5 level display
// Country – Province_State – City – Restaurant – Profile
var Country = { ‘Select country’:”,’Canada’:’Canada’,’Mexico’:’Mexico’,’United States’:’USA’ }
var USA = { ‘Select state’:”,’AL’:Alabama,’AK’:’Alaska’,’AZ’:’Arizona’,’AR’:’Arkansas’,’FL’:’Florida’ }
var Florida = { ‘Select city’:”,’Ft. Lauderdale’:’FTL’,’Miami’:’MIA’,’Tallahassee’:’TAL’ }
var FTL = { ‘Select restaurant’:”,’Burger King’:’ftlBK’:”,’Arby’s’:’ftlAR’ }
var ftlBK = { ‘Fast Food’:” }
var ftlAR = { ‘Fast Food’:” }
var MIA = { ‘Select restaurant’:”,’Burger King’:’miaBK’:”,’Arby’s’:’miaAR’ }
var miaBK = { ‘Fast Food’:” }
var miaAR = { ‘Fast Food’:” }
var TAL = { ‘Select restaurant’:”,’Krystal’:’talKR’:”,’Hardy’s’:’talHR’ }
var talKR = { ‘Fast Food’:” }
var talHR = { ‘Fast Food’:” }

function SetUp(SBox,level) { // alert(SBox+’n’+level);
switch(level) {
case ‘Country’: Populate(SBox,Country); break;
case ‘Canada’: Populate(SBox,Canada); break;

case ‘Mexico’: Populate(SBox,Mexico); break;

case ‘USA’: Populate(SBox,USA); break;

case ‘Florida’: Populate(SBox,Florida); break;

case ‘FTL’: Populate(SBox,FTL); break;

case ‘ftlBK’: Populate(SBox,ftlBK); break;

case ”: break;
default : alert(‘Invalid entry: ‘+level); break; // should never occur with good choices
}
}

<select id=”SBox0″ onchange=”SetUp(‘SBox1’,this.value)”></select>
<select id=”SBox1″ onchange=”SetUp(‘SBox2’,this.value)”></select>
<select id=”SBox2″ onchange=”SetUp(‘SBox3’,this.value)”></select>
<select id=”SBox3″ onchange=”SetUp(‘SBox4’,this.value)”></select>
<select id=”SBox4″></select>

*/
–>
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERauthorApr 28.2011 — Just discovered, on another thread of this forum, how to do this. ?

It appears to be a 2 line change and a lot of deleted code. :eek:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Passing Object Indirectly &lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?t=245681

var Transport = { 'Select':'','Air':'Air','Land':'Land','Sea':'Sea' };

var Air = { 'Select':'','Planes':'Planes','Motorized Ultralight':'Ultralight','Free-flight Glider':'Glider' };
var Planes = { 'Select':'','747':'','737':'','Cessna':'' };
var Ultralight = { 'Select':'','Kolb Flyer':'','Kitfox':'' };
var Glider = { 'Select':'','Schwietzer Glider':'' };

var Land = { 'Select':'','Car':'Car','Truck':'Truck','Train':'Train' };
var Car= { 'Select':'','BMW Z3':'','Corvette':'','Chevy Malibu':'','Lincoln LS':'' };
var Truck = { 'Select':'','F-150':'','S-10':'','Tahoe':'' };
var Train= { 'Selectl':'','Freight Train':'','Passenger Train':'','Steam':'' };

var Sea = { 'Select type':'','Ship':'Ship','Boat':'Boat','Submarine':'Submarine' };
var Ship = { 'Select':'','Oil Tanker':'','Cruise':'','Yacht':'' };
var Boat = { 'Select':'','Cris-Craft':'','Ski':'','Sail':'','Oar':'' };
var Submarine = { 'Select':'','Los Angeles Class':'','Kilo Class':'','Seawolf Class':'' };

function Populate(SBox,strObj) {
var SObj = window[strObj];
oSel = document.getElementById(SBox);
oSel.options.length = 0;
// optional display
// oSel.options[oSel.options.length] = new Option('Select an option','',false,false);
for (var x in SObj) {
oSel.options[oSel.options.length] = new Option(x,SObj[x], false,false);
}
oSel.selectedIndex = 0;
}

onload = function() { Populate('SBox0','Transport'); }
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;select id="SBox0" onchange="Populate('SBox1',this.value)"&gt;&lt;/select&gt;
&lt;select id="SBox1" onchange="Populate('SBox2',this.value)"&gt;&lt;/select&gt;
&lt;select id="SBox2"&gt;&lt;/select&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@JMRKERauthorApr 29.2011 — I have read elsewhere that using window[' '] is a poor method to use,

but I do not understand why that is

nor what would be a better method to use to achieve the desired results.

Anyone have an explanation or a better way?

I'm always looking for improvements to my proposed code!
Copy linkTweet thisAlerts:
@aj_nscApr 29.2011 — It's a poor method because you're using the global 'namespace' of Javascript where collisions can occur easily in an uncontrolled environment.

window['myvar'] = 'foo';

as far as I know is no different than

var myvar = 'foo'; (outside a function)


It's always a good idea to namespace your code if possible, to avoid collisions as best as possible like:

<i>
</i>var myNameSpace = {
myint : 10,
myfloat : 1.5,

<i> </i> mymethod = function() {
<i> </i> return this.myint*this.myfloat;
<i> </i> }

}

alert(myNameSpace.mymethod()); //alerts 15


In the above example, the only thing you have to worry about is a collision with another variable called myNameSpace....other than worrying collisions with the vars myint, myfloat, and mymethod if they were declared outside a namespace.
Copy linkTweet thisAlerts:
@rnd_meApr 29.2011 — I have read elsewhere that using window[' '] is a poor method to use,

but I do not understand why that is

nor what would be a better method to use to achieve the desired results.

Anyone have an explanation or a better way?

I'm always looking for improvements to my proposed code![/QUOTE]


if you want to avoid globals, simply wrap your existing code in an anon function, and close the event by directly assigning them inside the same function.

[B]EDIT: (working, clean example of what i meant)[/B]

[CODE]<html>
<head>
<title> Passing Object Indirectly </title>
</head>
<body>
<select id="SBox0" ></select>
<select id="SBox1" ></select>
<select id="SBox2"></select>



<script type="text/javascript">
// From: http://www.webdeveloper.com/forum/showthread.php?t=245681
(function(){
var objOptions={
Transport : { 'Select':'','Air':'Air','Land':'Land','Sea':'Sea' },

Air : { 'Select':'','Planes':'Planes','Motorized Ultralight':'Ultralight','Free-flight Glider':'Glider' },
Planes : { 'Select':'','747':'','737':'','Cessna':'' },
Ultralight : { 'Select':'','Kolb Flyer':'','Kitfox':'' },
Glider : { 'Select':'','Schwietzer Glider':'' },

Land : { 'Select':'','Car':'Car','Truck':'Truck','Train':'Train' },
Car: { 'Select':'','BMW Z3':'','Corvette':'','Chevy Malibu':'','Lincoln LS':'' },
Truck : { 'Select':'','F-150':'','S-10':'','Tahoe':'' },
Train: { 'Selectl':'','Freight Train':'','Passenger Train':'','Steam':'' },

Sea : { 'Select type':'','Ship':'Ship','Boat':'Boat','Submarine':'Submarine' },
Ship : { 'Select':'','Oil Tanker':'','Cruise':'','Yacht':'' },
Boat : { 'Select':'','Cris-Craft':'','Ski':'','Sail':'','Oar':'' },
Submarine : { 'Select':'','Los Angeles Class':'','Kilo Class':'','Seawolf Class':'' }
};

function Populate(SBox,strObj) {
var SObj = objOptions[strObj];
var oSel = document.getElementById(SBox);
oSel.options.length = 0;
// optional display
// oSel.options[oSel.options.length] = new Option('Select an option','',false,false);
for (var x in SObj) {
oSel.options[oSel.options.length] = new Option(x,SObj[x], false,false);
}
oSel.selectedIndex = 0;
}

function el(tid) {return document.getElementById(tid);}

el("SBox0").onchange=function(){Populate('SBox1',this.value)};
el("SBox1").onchange=function(){Populate('SBox2',this.value)};
Populate('SBox0','Transport');

}());//end wrapper



</script>

</body>
</html>[/CODE]


i did three find and replaces:
[CODE]
"var " > "",
"=" > ":",
";" > ","
[/CODE]
Copy linkTweet thisAlerts:
@JMRKERauthorApr 29.2011 — Thank you 'aj_nsc' for the explanation of the caution

and

Thank you 'rnd_me' for the example that seems to fit the explanation!

It makes the creation of multi-level drop down selections a lot easier for me to understand!

Appreciate the learning experience. :eek:??
Copy linkTweet thisAlerts:
@JMRKERauthorApr 29.2011 — if you want to avoid globals, simply wrap your existing code in an anon function, and close the event by directly assigning them inside the same function.

[B]EDIT: (working, clean example of what i meant)[/B]

...

i did three find and replaces:
[CODE]
"var " > "",
"=" > ":",
";" > ","
[/CODE]
[/QUOTE]


Thanks again for code and the corrections ?

... as usual my lightning fast fat fingers failed me again! :eek:
×

Success!

Help @JMRKER spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...