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

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

JiPiii
1#
JiPiii Published in 2017-12-07 07:44:05Z

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:

<fieldset>
<p>
<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></span> 

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

<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 
 </select></span> 
 <input type="submit" onclick="myCarBrandAndTypeFunction()" value="Submit">
 </p>
 </fieldset>

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":

    <script> 

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 = "";        
      };
     </script> 

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.

cdoshi
2#
cdoshi Reply to 2017-12-07 08:35:13Z

I am not entirely sure of what you want but here is my solution:

myCarPriceCheckBuilderObjectNew.one = eval(myCarPriceCheckBuilder['seventh']());

You also would need to define the objects sedan_car_audi and similar to get values else it will throw an error. Hope that answers!

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO