# Deselect ion-segment

**URL:** <https://forum.ionicframework.com/t/deselect-ion-segment/243804>\
**Category:** Ionic Vue\
**Created:** [August 23, 2024, 1:00pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804 "2024-08-23T13:00:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 1:00pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/1 "2024-08-23T13:00:43Z")

</div>

@ionic/vue": "^8.0.0  
“vue”: “^3.3.0”,

This is my code for IonSegment. I know about the events @ion-change and @ion-select, the problem is that both trigger only if the value is “new”, so if you click twice on the same button, the second time it doesn’t trigger. So I resulted to having a click function on each button and having a variable that holds the previous model value. First, this is my component:

```auto
// I know I can have v-for but just so u see more clearly the values
<ion-segment ref="ionSegmentRef" v-model="calorieSpan">
     <ion-segment-button value="lowerThan" @click="segmentClicked">do 350</ion-segment-button>
     <ion-segment-button value="fromTo" @click="segmentClicked">350-500</ion-segment-button>
     <ion-segment-button value="higherThan" @click="segmentClicked">od 500</ion-segment-button>
</ion-segment>

```

I am using v-model but I did try with :value=calorieSpan and it doesn’t work either. This is my click function:

```auto
 segmentClicked() {
            if (this.previousValue === this.calorieSpan) {
                console.log("making null")
                this.calorieSpan = null
                // this is just outta desperation, tried with modelValue and value too
                this.$refs.ionSegmentRef.$el.modelvalue = null
                this.previousValue = null
            } else this.previousValue = this.calorieSpan
        }

```

So like this, it does make the value null, however the button doesn’t “un-check” it seemingly stays active. In order to de-select it I would have to dig deep and remove the styles which just overall with all the rest sounds so stupid to do.

Am I doing something wrong? Shouldn’t it just deselect when I null the v-model? It is unchecked when it mounts but once you click on one option there is no way to deselect anymore.

Cant we add something like there is on the radio group? allow-empty-selection? Or am I messing up somewhere? Please help.

![showcase](https://global.discourse-cdn.com/ionicframework/original/3X/8/d/8d8b46c103f32662a3f987da5ddc1cc973f3cabc.gif)

Pls heeeelp

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [August 23, 2024, 1:13pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/2 "2024-08-23T13:13:26Z")

</div>

Interesting use case! I would argue you are using `ion-segment` for an unintended use case. Just my opinion though as another Ionic Vue user 😄 It seems they were designed to always have one selected.

What if you just add an additional option “None”?

---

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 1:23pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/3 "2024-08-23T13:23:37Z")

</div>

You’re probably right, I just noticed it actually doesn’t null the value at all? I put {{ calorieSpan }} above my component to check if the value changed and even after the click function that should make it null function runs it remains the same 😭 what a shame, I love the styling of this.

![video3](https://global.discourse-cdn.com/ionicframework/original/3X/6/d/6d8c86dbf1de621caba577e383a6621f2ebfdafd.gif)

---

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 1:38pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/4 "2024-08-23T13:38:38Z")

</div>

As for the other None option, I tried adding a forth `display: none` option that I would “click” programatically but the button itself is in a shadow root 😃 so I just don’t how to access it, this is as far as I got (it’s included in the making null part of the click function):

```auto
this.$refs.ionSegmentRef.$el.lastElementChild.click()

```

which doesn’t work cause that’s not the button. But the more I try to more I think I will just do it right 😃 and style a radio group

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [August 23, 2024, 1:44pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/5 "2024-08-23T13:44:25Z")

</div>

I agree, it does look nice!

Having a hidden one is a good idea! Can’t you just set the `value` of the `ion-segment` to the value of the “none” `ion-segment-button`? That is how I programtically set the selected button.

---

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 2:01pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/6 "2024-08-23T14:01:09Z")

</div>

I tried to do this:

```auto
<ion-segment ref="ionSegmentRef" :value="calorieSpan">
     <ion-segment-button value="lowerThan" @click="segmentClicked">do 350</ion-segment-button>
     <ion-segment-button value="fromTo" @click="segmentClicked">350-500</ion-segment-button>
     <ion-segment-button value="higherThan" @click="segmentClicked">od 500</ion-segment-button>
     <ion-segment-button value="test" class="d-none" @click="segmentClicked">od 500</ion-segment-button>
</ion-segment>

```

And like this it doesn’t change the calorieSpan variable at all when clicking ☹ it does change probably within the segment because the active buttons keep moving as I am clicking but my calorieSpan variable doesn’t react and stays the same

I also tried this `this.$refs.ionSegmentRef.$el.value = "test"` and it didn’t change to the button, I tried to also remove the `d-none` class (which is `display: none`) and it still wouldn’t change it

At this point I feel like the ion-segment has some personal beef with me 😃

---

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 5:12pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/7 "2024-08-23T17:12:21Z")

</div>

For anyone facing the same issue I created this:

```auto
<ion-grid class="ion-no-padding">
    <ion-row ref="calorieButtonRow" class="ion-btn-row">
        <ion-col v-for="(item, key) in calorieSpans" :key="key" cols="4">
            <ion-button
                :id="key"
                selected="false"
                :border="key === 'fromTo'"
                expand="block"
                color="white"
                class="mx-0"
                @click="optionSelected(key)"
                >{{ item.kcal }}
            </ion-button>
        </ion-col>
    </ion-row>
</ion-grid>

<style scoped>
.ion-btn-row {
    background-color: rgba(var(--ion-color-caloriespan-rgb));
    border-radius: 10px;
    padding-left: 2.5px;
    padding-right: 2.5px;
}
ion-button {
    --background-hover: rgba(var(--ion-color-caloriespan-rgb));
    --background-activated: rgba(var(--ion-color-caloriespan-rgb));
    margin-top: 2.5px;
    margin-bottom: 2.5px;
}
ion-button[selected="false"] {
    --color-hover: rgba(var(--ion-color-terShade5-rgb));
}
ion-button[selected="false"]::part(native) {
    background-color: rgba(var(--ion-color-caloriespan-rgb));
}
ion-button[selected="true"]::part(native) {
    background-color: rgba(var(--ion-color-white-rgb));
}
ion-button[border="true"] {
    border-right: 1px solid rgba(var(--ion-color-terShade2-rgb));
    border-left: 1px solid rgba(var(--ion-color-terShade2-rgb));
}
</style>

```

The calorieSpan color is

```auto
--ion-color-caloriespan: #eee;
--ion-color-caloriespan-rgb: 238, 238, 238;

```

This part is to put the border only on the middle element `:border="key === 'fromTo'"`

And finally this is the function

```auto
optionSelected(key) {
            const el = document.getElementById(key)
            const isAlreadySelected = el.getAttribute("selected") === "true"
            if (isAlreadySelected) // code for when the btn is already selected 
            else {
                const previous = this.$refs.calorieButtonRow.$el.querySelector('[selected="true"]')
                if (previous) previous.setAttribute("selected", false)
                // additional code for selecting the btn
            }
            el.setAttribute("selected", !isAlreadySelected)
        },

```

![gid3](https://global.discourse-cdn.com/ionicframework/original/3X/9/9/99d926008decf1676b6056a99947dd453a1cccd1.gif)

Thank you for all your help :))

---

<div class="post-metadata">

**Author:** ![twestrick](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/twestrick/32/136131_2.png) [@twestrick](https://forum.ionicframework.com/u/twestrick)\
**Post date:** [August 23, 2024, 5:30pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/8 "2024-08-23T17:30:55Z")

</div>

Well, I just figured it out for you with `ion-segment` 😁

I think the missing key was not stopping propagation of the click event.

> **[IonSegmentButton De-select - StackBlitz](https://stackblitz.com/edit/gwmbwa?file=src%2Fcomponents%2FExample.vue)**
>
> https://forum.ionicframework.com/t/deselect-ion-segment/243804

```html
<ion-segment :value="selected">
  <ion-segment-button value="all" @click.prevent.stop="handleButtonClick">
    <ion-label>All</ion-label>
  </ion-segment-button>
  <ion-segment-button
    value="favorites"
    @click.prevent.stop="handleButtonClick"
  >
    <ion-label>Favorites</ion-label>
  </ion-segment-button>
  <ion-segment-button
    value="no-favorites"
    @click.prevent.stop="handleButtonClick"
  >
    <ion-label>Not My Favorites</ion-label>
  </ion-segment-button>
  <ion-segment-button value="none" style="display: none">
    <ion-label>None</ion-label>
  </ion-segment-button>
</ion-segment>

```

```javascript
const selected = ref('all');

function handleButtonClick(event: PointerEvent): void {
  const newValue = event.target.value;

  if (newValue === selected.value) {
    selected.value = 'none';
  } else {
    selected.value = newValue;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Asko-Dev](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/asko-dev/32/147930_2.png) [@Asko-Dev](https://forum.ionicframework.com/u/Asko-Dev)\
**Post date:** [August 23, 2024, 9:48pm UTC](https://forum.ionicframework.com/t/deselect-ion-segment/243804/9 "2024-08-23T21:48:00Z")

</div>

Omggg you’re a genius! That’s so much better than mine! Marking this as the solution and implementing asap 🤩 thank youuu SO MUCH
