Home Can't get dynamic variable name to get result on document.getElementById, only returning the string
Reply: 0

Can't get dynamic variable name to get result on document.getElementById, only returning the string

user4913 Published in April 27, 2018, 6:42 am

I'm new to JS and been trying for some time already to solve this myself, but after lots of searching (found lots of information on different ways to concat or otherwise make variables dynamic but not how to use these variables to retrieve properties) and trying I finally decided to ask for help.

I have been trying to use dynamic variables to get properties of an object, first outputting my information to JS objects, and after that getting values from dropdown select elements and a string, combining all that information to one string, and after that using that string as object.property. But what I finally get as a result is just output string which seems correct, but it's printed out just as a string, not working as object.property. Propably this is just something small I just can't figure out, or perhaps my approach is just totally wrong. Also already tried the same by outputting to hidden table elements, and then changing hidden=false, but the result was more or less the same. By hardcoding the string to work as the object.property I get the result I'm looking for, this done just to test my objects.

Using object constructor that seems working ok:

 function myCar(carClass, id, brandType, price) {
     this.carClass = carClass;
     this.id = id;
     this.brandType = brandType;
     this.price = price;

When fetching database information myCar or object variable name gets to be for example "sedan_car_audi", and other information goes in their respective places.

The dropdown information comes from here:

<span>myCarType Title: <select name="myCarType" id="myCarTypeId" onchange="clearSelectionOutput()" > 
 <option value="sedan" > sedan
 <option value="hatchback" > hatchback
 <option value="convertible" > convertible
 <option value="customized" > customized     

<select name="vehicleType" id="vehicleType" hidden> 
<option value="car">

<span>myCarBrand Title: <select name="myCarBrand"id="myCarBrandId" onchange="clearSelectionOutput()" >
 <option value="audi" > Audi
 <option value="bmw"   > BMW    
 <option value="mercedenz" > Mercedenz
 <option value="volvo"    > Volvo 
 <input type="submit" onclick="myCarBrandAndTypeFunction()" value="Submit">

And script where I try to make the combined variable( or actually here are couple of ways I've tried) and use document.getElementById to get the result for example sedan_car_audi.price , but what I get is only the string "sedan_car_audi.price" as the result, not the price property value.. (which works ok for showing it on the "demo10":


function myCarBrandAndTypeFunction(){
 var myCarTypeIdVariable =document.getElementById("myCarTypeId");
 var myCarBrandIdVariable =document.getElementById("myCarBrandId");
 var vehicleTypeVariable =document.getElementById("vehicleType");

 var underscore ="_";
 var priceAddendum = ".price";

 var myCarPriceCheckBuilder = {
     first: myCarTypeIdVariable.value,
     second: underscore,
     third: vehicleType.value,
     fourth: underscore,
     fifth: myCarBrandIdVariable.value,
     sixth: priceAddendum,
     seventh: function () {return this.first + this.second + this.third + this.fourth + this.fifth + this.sixth}

     var myCarPriceCheckBuilderAlltogether =  myCarTypeId.value + underscore + vehicleType.value + underscore + myCarBrandId.value + priceAddendum;
     var myCarPriceCheckBuilderStraight = myCarTypeId.value + "_" + vehicleType.value + "_" + myCarBrandId.value;    
     var myCarPriceCheckBuilderObjectNew = {one : ""};
        myCarPriceCheckBuilderObjectNew.one = myCarPriceCheckBuilder['seventh'] ;

         document.getElementById("demo10").innerHTML = sedan_car_audi.price + " €";      
         document.getElementById("demo11").innerHTML = "Your car price is " + myCarPriceCheckBuilderAlltogether + " €"; 
         document.getElementById("demo13").innerHTML = "My New Car's price is " + myCarPriceCheckBuilder.seventh + " €"  ;  

         function clearSelectionOutput(){
         document.getElementById("demo10").innerHTML = "";
         document.getElementById("demo11").innerHTML = "";
         document.getElementById("demo12").innerHTML = ""; 
         document.getElementById("demo13").innerHTML = ""; 
         document.getElementById("demo14").innerHTML = "";        

I hope this is clear enough, this is modified version of my real code, but should be accurately copied/modified.

Sleepless nights and grayed hair would hugely appreciate any help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316187 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO