Jump to content

Not getting the value i need...


Drongo_III

Recommended Posts

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/>");

} 

Link to comment
Share on other sites

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 + "
");
}

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.