Drongo_III Posted May 18, 2012 Share Posted May 18, 2012 Hi Guys Bit of a noob question but i'm really rusty on javascript. I'm working on a simple example that is gonig to be used to create somethnig more complex. Anyway essentially i have a loop (below) and the intention is that it will append (to a paragraph) all the values from a few objects i've created. The issue is instead of appending the actual values i simply get the literal - e.g. the value of itm2.model should come out as 'boxter' but instead it gets apended as itm2.model. So how can i force it to interpret it as the value? It's quite hard to explain so i hope that gives you the idea. itm2= new Object(); itm2.make="porche"; itm2.model="boxter"; var g = <?php echo $count?>; for(i=1; i<=g; i++){ var handle = "itm" + i +'.model' ; $('#writer').append(handle + "<br/>"); } Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/ Share on other sites More sharing options...
requinix Posted May 18, 2012 Share Posted May 18, 2012 Just like in PHP, don't use multiple numbered variables - use an array. // create the various objects var porche = new Object(); porche.make = "porche"; porche.model = "boxter"; // put them into an array itm = [porche]; // then loop over it for (var k in itm) { $("#writer").append(itm[k].model + " "); } Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346644 Share on other sites More sharing options...
.josh Posted May 18, 2012 Share Posted May 18, 2012 I agree with requinix but to more directly answer your question, the problem is that this: var handle = "itm" + i +'.model' ; just makes a string with a literal value of "item2.model". In order to get it assign a value like that, you would have to wrap it in an eval() call, which will cause whatever string you pass to eval() to be executed as javascript. But you don't really wanna do that. At best, it is bad practice. At worst, it opens up all kinds of potential security holes. Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346688 Share on other sites More sharing options...
.josh Posted May 18, 2012 Share Posted May 18, 2012 hmm also I don't think requinix's example quite matches what you are trying to do. I think what you are looking for is the following: <script type='text/javascript'> var itm = [ {"make" : "porche", "model" : "boxter"}, {"make" : "mazda", "model" : "protege"} ]; var g = <?php echo $count?>; for(i=1; i<=g; i++){ $('#writer').append(itm[i].model + "<br/>"); } </script> Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346691 Share on other sites More sharing options...
requinix Posted May 18, 2012 Share Posted May 18, 2012 In order to get it assign a value like that, you would have to wrap it in an eval() call, which will cause whatever string you pass to eval() to be executed as javascript. You wouldn't have to use eval() for it. Just access it through the window object. window["itm" + i].model Ah, the flexibility of JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346705 Share on other sites More sharing options...
Drongo_III Posted May 19, 2012 Author Share Posted May 19, 2012 Ha! easy when you know how. Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346790 Share on other sites More sharing options...
.josh Posted May 19, 2012 Share Posted May 19, 2012 In order to get it assign a value like that, you would have to wrap it in an eval() call, which will cause whatever string you pass to eval() to be executed as javascript. You wouldn't have to use eval() for it. Just access it through the window object. window["itm" + i].model Ah, the flexibility of JavaScript. Oh yeah...duh...i even wrote a script the other day doing that...can't believe that slipped my mind...you win again old age Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346819 Share on other sites More sharing options...
.josh Posted May 19, 2012 Share Posted May 19, 2012 But as you pointed out, he should be using an array anyways Quote Link to comment https://forums.phpfreaks.com/topic/262735-not-getting-the-value-i-need/#findComment-1346820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.