Hiding an element if another element is undefined

Hey all, I have another issue…

Basically I have 14 text boxes, each box draws its content from an external txt file. Next to each text box is a button. I want the button to be either visible=true or visible=false depending on weather or not the text box has content or is “undefined” Here is what I have so far, and it’s not working:

loadText = new LoadVars();
loadText.load(cat1+"_"+cat2+"_"+cat3+"_"+cat4+"_products.txt");
loadText.onLoad = function() {
    product1.text = this.product1;
    product2.text = this.product2;
    product3.text = this.product3;
    product4.text = this.product4;
    product5.text = this.product5;
    product6.text = this.product6;
    product7.text = this.product7;
    product8.text = this.product8;
    product9.text = this.product9;
    product10.text = this.product10;
    product11.text = this.product11;
    product12.text = this.product12;
    product13.text = this.product13;
    product14.text = this.product14;
};
for (x=1, x<14, x++){
  if (this.product+x == undefined) {
     this.send+x+_btn._visible = false;
  } else {
     this.send+x+_btn._visble = true;
  }
}

So as you can see, I would like a FOR loop to just run through the variables and check to see if any of them are undefined. if it is, then make the correct send button invisible.

any ideas?

Oh ya, once this is solved, I have basically the same issue in another section. Basically, if an image is undefined, I want the button to be invisible.


for (x=1; x<14; x++){
  if (this["product"+x] == undefined) {
     this["send"+x+"_btn"]._visible = false;
  } else {
     this["send"+x+"_btn"]._visible = true;
  }
}

hmm, I just tried that, and I got some errors:


**Error** Symbol=slider, layer=Layer 3, frame=1:Line 19: ';' expected
     for (x=1, x<14, x++){

**Error** Symbol=slider, layer=Layer 3, frame=1:Line 20: ';' expected
       if (this["product"+x] == undefined) {

**Error** Symbol=slider, layer=Layer 3, frame=1:Line 22: 'else' encountered without matching 'if'
       } else {

**Error** Symbol=slider, layer=Layer 3, frame=1:Line 25: Unexpected '}' encountered
     }

Total ActionScript Errors: 4      Reported Errors: 4

any ideas?

no you tried the wrong thing…note the error for (x=1, x<14, x++){

it should be for (x=1**[COLOR=“Red”];[/COLOR]** x<14**[COLOR=“Red”];[/COLOR]** x++){

recopy the AS i posted above…

well, that stopped the errors, but the script isn’t working correctly.

For testing purposes, I made the product2 variable in the text file undefined.

The flash movie shows “undefined” in the product2 text box… but the send2_btn is still there.

I think I need to also add a line that says “if the product box is undefined, dont show the product box or the send button” like:


if (this["product"+x] == undefined) {
this["product"+x]._visible = false;
this["send"+x+"_btn"]._visible = false;
}

but that doesn’t work either… ugh… what am I doing wrong?

so I thought that maybe I needed to define another variable in the loop for it to work… but its still not working correctly. Basically the loop is either making everything invisible, or everything visible. I can’t get it to find the undefined element and hide only the correct objects.


for (x=1; x<14; x++){
    var definer = this["product"+x].text;
if (definer == undefined) {
    this["product"+x]._visible = false;
    this["send"+x+"_btn"]._visible = false;
    } else {
        this["product"+x]._visible = true;
        this["send"+x+"_btn"]._visible = true;
    }
}

var loadText:LoadVars = new LoadVars();
loadText.load(cat1 + "_" + cat2 + "_" + cat3 + "_" + cat4 + "_products.txt");
loadText.onLoad = mx.utils.Delegate.create(this, function () {
 for (var x:Number = 1; x < 14; x++) {
  this["product" + x].text = this.loadText["product" + x];
  if (this.loadText["product" + x] == undefined) {
   this["send" + x + "_btn"]._visible = false;
  } else {
   this["send" + x + "_btn"]._visible = true;
  }
 }
});

Should work if you are using AS2 :slight_smile:

Excellent! That worked well! I just had to add a " this[“product” + x].text = " “;” so the “undefined” wouldn’t display.

Thanks a lot!