Question: I have three 3 divs

```
<div id= "product_1"></div>
<div id= "product_2"></div>
<div id= "product_3"></div>
<div id= "product_4"></div>
<div id= "product_5"></div>

```

I am changing the ids dynamically 

```
var hotelCode = "CUNMXHIDD,CUNMXMAYA,CUNMXDSAN"
var splittedHotelCode = hotelCode.toString().split(',');
jQuery.each(splittedHotelCode, function(i, hotelCode) {
 $("#product_"+ i).attr("id","product_"+ hotelCode);
});

```

After this I want to hide divs which are not been indexed product\_4 and product\_5

Now DOM is 

```
<div id= "product_CUNMXHIDD"></div>
<div id= "product_CUNMXMAYA"></div>
<div id= "product_CUNMXDSAN"></div>
<div id= "product_4"></div>
<div id= "product_5"></div>

```

I dont want to hard code. Is it possible I can hide them by Jquery ?

Answer: 
You could use the `length` of the array and the `slice` method.

```
$('div[id^=product]').slice(splittedHotelCode.length).hide();

```
1. Keep track of index to preserve the last index
2. Use [**:gt()**](https://api.jquery.com/gt-selector/) to select and then hide all elements that have id that start with product\_

---

```
var hotelCode = CUNMXHIDD,CUNMXMAYA,CUNMXDSAN
var splittedHotelCode = hotelCode.toString().split(',');
var lastIndex = 0;
jQuery.each(splittedHotelCode, function(i, hotelCode) {
 $("#product_"+ i).attr("id","product_"+ hotelCode);
 lastIndex = i;
});

$('[id^="product_"]:gt('+lastIndex+')').hide();

```