# Hiding an element if another element is undefined

**URL:** https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498
**Category:** flash
**Created:** [April 13, 2006, 6:33pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498 "2006-04-13T18:33:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 13, 2006, 6:33pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/1 "2006-04-13T18:33:41Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 13, 2006, 6:39pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/2 "2006-04-13T18:39:03Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)
#### Post date: [April 13, 2006, 6:53pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/3 "2006-04-13T18:53:16Z")

</div>

```auto

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

```

---

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 13, 2006, 6:56pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/4 "2006-04-13T18:56:57Z")

</div>

> [@matthewjumps](#):
>
> ```
> ActionScript Code:
> [FONT=Courier New][LEFT][COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR]x=[COLOR=#000080]1[/COLOR]; x&lt;[COLOR=#000080]14[/COLOR]; x++[COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]
> 
> ```
> 
> [COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“product”[/COLOR]+x[COLOR=#000000]][/COLOR] == [COLOR=#0000FF]undefined[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“send”[/COLOR]+x+[COLOR=#FF0000]“\_btn”[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_visible[/COLOR] = [COLOR=#000000] **false** [/COLOR];  
> [COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“send”[/COLOR]+x+[COLOR=#FF0000]“\_btn”[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_visible[/COLOR] = [COLOR=#000000] **true** [/COLOR];  
> [COLOR=#000000]}[/COLOR]  
> [COLOR=#000000]}[/COLOR]  
> [/LEFT]  
> [/FONT]

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

```auto

**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?

---

<div class="post-metadata">

### Author: ![matthewjumps](https://avatars.discourse-cdn.com/v4/letter/m/49beb7/32.png) [@matthewjumps](https://forum.kirupa.com/u/matthewjumps)
#### Post date: [April 13, 2006, 7:06pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/5 "2006-04-13T19:06:32Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 13, 2006, 7:25pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/6 "2006-04-13T19:25:39Z")

</div>

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:

```auto

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?

---

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 13, 2006, 8:04pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/7 "2006-04-13T20:04:59Z")

</div>

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.

```auto

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;
    }
}

```

---

<div class="post-metadata">

### Author: ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)
#### Post date: [April 14, 2006, 12:12am UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/8 "2006-04-14T00:12:45Z")

</div>

```auto
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 🙂

---

<div class="post-metadata">

### Author: ![scopa](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@scopa](https://forum.kirupa.com/u/scopa)
#### Post date: [April 14, 2006, 6:16pm UTC](https://forum.kirupa.com/t/hiding-an-element-if-another-element-is-undefined/185498/9 "2006-04-14T18:16:31Z")

</div>

> [@TheCanadian](#):
>
> ```
> ActionScript Code:
> [FONT=Courier New][LEFT][COLOR=#000000] **var** [/COLOR] loadText:[COLOR=#0000FF]LoadVars[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]LoadVars[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
> 
> ```
> 
> loadText.[COLOR=#0000FF]load[/COLOR][COLOR=#000000]([/COLOR]cat1 + [COLOR=#FF0000]“_“[/COLOR] + cat2 + [COLOR=#FF0000]”_”[/COLOR] + cat3 + [COLOR=#FF0000]“\_”[/COLOR] + cat4 + [COLOR=#FF0000]“\_products.txt”[/COLOR][COLOR=#000000])[/COLOR];  
> loadText.[COLOR=#0000FF]onLoad[/COLOR] = mx.[COLOR=#000080]utils[/COLOR].[COLOR=#000080]Delegate[/COLOR].[COLOR=#000080]create[/COLOR][COLOR=#000000]([/COLOR][COLOR=#0000FF]this[/COLOR], [COLOR=#000000] **function** [/COLOR] COLOR=#000000[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] x:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000080]1[/COLOR]; x \< [COLOR=#000080]14[/COLOR]; x++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“product”[/COLOR] + x[COLOR=#000000]][/COLOR].[COLOR=#0000FF]text[/COLOR] = [COLOR=#0000FF]this[/COLOR].[COLOR=#000080]loadText[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“product”[/COLOR] + x[COLOR=#000000]][/COLOR];  
> [COLOR=#0000FF]if[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#0000FF]this[/COLOR].[COLOR=#000080]loadText[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“product”[/COLOR] + x[COLOR=#000000]][/COLOR] == [COLOR=#0000FF]undefined[/COLOR][COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“send”[/COLOR] + x + [COLOR=#FF0000]“\_btn”[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_visible[/COLOR] = [COLOR=#000000] **false** [/COLOR];  
> [COLOR=#000000]}[/COLOR] [COLOR=#0000FF]else[/COLOR] [COLOR=#000000]{[/COLOR]  
> [COLOR=#0000FF]this[/COLOR][COLOR=#000000][[/COLOR][COLOR=#FF0000]“send”[/COLOR] + x + [COLOR=#FF0000]“\_btn”[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#0000FF]\_visible[/COLOR] = [COLOR=#000000] **true** [/COLOR];  
> [COLOR=#000000]}[/COLOR]  
> [COLOR=#000000]}[/COLOR]  
> [COLOR=#000000]}[/COLOR][COLOR=#000000])[/COLOR];  
> [/LEFT]  
> [/FONT]
> 
> Should work if you are using AS2 🙂

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

Thanks a lot!
