# show or hide entity

**URL:** https://community.cesium.com/t/show-or-hide-entity/3868
**Category:** CesiumJS
**Created:** [March 16, 2016, 1:52pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868 "2016-03-16T13:52:34Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [March 16, 2016, 1:52pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868/1 "2016-03-16T13:52:34Z")

</div>

Hi all!

I'd like to add show/hide feature for entity in my application. I'd also like to control this action with checkbox. Below is my experiment.

if (checkbox\_nn.checked){  
.  
.  
.  
&nbsp;&nbsp;var redLine = viewer.entities.add({  
&nbsp;&nbsp;&nbsp;&nbsp;name : 'Red line on the surface',  
&nbsp;&nbsp;&nbsp;&nbsp;polyline : {  
&nbsp;&nbsp;&nbsp;&nbsp;positions : Cesium.Cartesian3.fromDegreesArray(array\_of\_coordinates),  
&nbsp;&nbsp;&nbsp;&nbsp;width : 3,  
&nbsp;&nbsp;&nbsp;&nbsp;material : Cesium.Color.RED  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;});  
}  
else {  
&nbsp;&nbsp;viewer.entities.remove(redLine); //this is not working  
}

Help please. Thank you in front!

---

<div class="post-metadata">

### Author: ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)
#### Post date: [March 16, 2016, 2:33pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868/2 "2016-03-16T14:33:48Z")

</div>

Hello,

Instead of adding and removing your entities, you can simply change the value of the show property for the entity. Here is an example:

var viewer = new Cesium.Viewer(‘cesiumContainer’);

var redLine = viewer.entities.add({  
polyline : {  
positions : Cesium.Cartesian3.fromDegreesArray([-75, 35,  
-125, 35]),  
width : 5,  
material : Cesium.Color.RED  
}  
});

Sandcastle.addToolbarButton(‘Toggle Show’, function() {  
redLine.show = !redLine.show;  
});

viewer.zoomTo(viewer.entities);

``

Best,

Hannah

---

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [March 17, 2016, 1:05pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868/3 "2016-03-17T13:05:08Z")

</div>

I've tried and i got error:

"Sandcastle-header.js:40 Uncaught TypeError: Cannot read property 'appendChild' of null"

in my web browser.

Do I have to change sandcastle-header.js?

---

<div class="post-metadata">

### Author: ![hannah](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/hannah/32/4509_2.png) [@hannah](https://community.cesium.com/u/hannah)
#### Post date: [March 17, 2016, 3:23pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868/4 "2016-03-17T15:23:45Z")

</div>

That might be happening because I used Sandcastle.addToolbarButton to add a button to the example. This code runs in our Sandcastle app: [http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Hello%20World.html&label=Showcases](http://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Hello%20World.html&label=Showcases)  
The code will work in your app if you remove the sandcastle specific function and call redLine.show = !redLine.show; from somewhere in your app.

Best,

Hannah

---

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [March 22, 2016, 10:52am UTC](https://community.cesium.com/t/show-or-hide-entity/3868/5 "2016-03-22T10:52:32Z")

</div>

It's not working. It says: "redLine is not defined"

Here's the part of code. I'm using jquery.ajax to fetch data from database using jsonp

if (checkbox\_nn.checked){  
&nbsp;&nbsp;jQuery.ajax({  
&nbsp;&nbsp;&nbsp;&nbsp;type: 'GET',  
&nbsp;&nbsp;&nbsp;&nbsp;url: url2,  
&nbsp;&nbsp;&nbsp;&nbsp;dataType: 'jsonp',  
&nbsp;&nbsp;&nbsp;&nbsp;jsonp: "cb",  
&nbsp;&nbsp;&nbsp;&nbsp;jsonpCallback: "jQ2",  
&nbsp;&nbsp;&nbsp;&nbsp;data:{x:x,  
&nbsp;&nbsp;&nbsp;&nbsp;y:y  
&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;crossDomain:true,  
&nbsp;&nbsp;&nbsp;&nbsp;cache:false,  
&nbsp;&nbsp;&nbsp;&nbsp;beforeSend : function(jqXHR, settings){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var url2 = settings.url;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var url2 = url2.replace("&\_=","&ts=");  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(url2);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jqXHR.url= url2;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;settings.url = url2;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[//console.log](https://console.log)(settings.url);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;&nbsp;success: function(data){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//drawing of low voltage layer  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var i=0; i \< data.length; i++){  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (var l=0; l\<data[i].POINT.length; l++){  
&nbsp;&nbsp;&nbsp;&nbsp;array\_of\_coordinates.push(data[i].POINT[l].p\_x);  
&nbsp;&nbsp;&nbsp;&nbsp;array\_of\_coordinates.push(data[i].POINT[l].p\_y);  
&nbsp;&nbsp;}  
&nbsp;&nbsp;var redLine = viewer.entities.add({  
&nbsp;&nbsp;&nbsp;&nbsp;name : 'Red line',  
&nbsp;&nbsp;&nbsp;&nbsp;polyline : {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;positions:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cesium.Cartesian3.fromDegreesArray(array\_of\_coordinates),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width : 2,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;material : Cesium.Color.RED  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array\_of\_coordinates=;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;},  
&nbsp;&nbsp;&nbsp;error: function (jqXHR, status, error) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(status, error);  
&nbsp;&nbsp;&nbsp;}   
&nbsp;&nbsp;});  
&nbsp;&nbsp;}  
else{  
&nbsp;&nbsp;redLine.show = !redLine.show;  
};

---

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [March 24, 2016, 8:40am UTC](https://community.cesium.com/t/show-or-hide-entity/3868/6 "2016-03-24T08:40:23Z")

</div>

Until i find better solution, i'll use

> else {

&nbsp;&nbsp;&nbsp;&nbsp;viewer.entities.removeAll(); //THIS

---

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [May 24, 2016, 7:56am UTC](https://community.cesium.com/t/show-or-hide-entity/3868/7 "2016-05-24T07:56:27Z")

</div>

Hi!

The problem remains. I still want to remove polyline according to checkbox state.

I have my code

if(something.checked){  
var yellowLine = viewer.entities.add...  
}else{  
viewer.entities.remove(yellowLine);  
}

and in my browser i got error:  
yellowLine is not defined Uncaught reference error

Any one any idea?!

---

<div class="post-metadata">

### Author: ![mbunic151](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@mbunic151](https://community.cesium.com/u/mbunic151)
#### Post date: [May 25, 2016, 7:59am UTC](https://community.cesium.com/t/show-or-hide-entity/3868/8 "2016-05-25T07:59:17Z")

</div>

I'm on the half way to get this done. The problem is in the scope of variable

If(checkbox.checked){  
// jquery sets variable 'yellowLine'  
}

If(!checkbox.checked){  
viewer.entities.remove(yellowLine) //doesn't see the 'yellowLine'  
}

Does anyone hava an idea?

---

<div class="post-metadata">

### Author: ![HQN](https://avatars.discourse-cdn.com/v4/letter/h/e19b73/32.png) [@HQN](https://community.cesium.com/u/HQN)
#### Post date: [June 8, 2019, 2:07pm UTC](https://community.cesium.com/t/show-or-hide-entity/3868/9 "2019-06-08T14:07:32Z")

</div>

Thanks, Hannah for your solution. I tried your method and I was able to either completely remove the redline or hide it.

Best!
