Help with tags!

Hello guys, I have a selection menu working with tags. I would like that when I select a new voice, it will automatically deselect the previous one and then update the content. Now it just adds the new products even if the tag is not the one selected. Can anyone help me?
The code that I already have is this:

import wixData from ‘wix-data’ ;

const databaseName = ‘Portainnesti’ ;
const databaseField = ‘tipo’ ;

$w . onReady ( function () {

$w ( '#selectionTags1' ). onChange (( event ) => { 
    **const**  selectedTag  =  $w ( '#selectionTags1' ). value ; 
    addItemstoRepeater ( selectedTag ); 
}) 

});

function addItemstoRepeater ( selectedOption = ) {

**let**  dataQuery  =  wixData . query ( databaseName ); 

**if**  ( selectedOption . length  >  0 ) { 
    dataQuery  =  dataQuery . hasSome ( databaseField ,  selectedOption ); 
} 

dataQuery 
    . find () 
    . then ( results  => { 
        **const**  filtereditemsReady  =  results . items ; 
        $w ( '#repeater1' ). data  =  filtereditemsReady ; 
    }) 

}

$w . onReady ( function () {

$w ( '#selectionTags1' ). onChange (()=>{ 

let VALUE = $w ( ‘#selectionTags1’ ). value
let LENGTH = VALUE . length

    console . log ( LENGTH ) 

for ( var i = 0 ; i < LENGTH - 1 ; i ++) {
if ( LENGTH > 1 ) {
VALUE . shift ()

        } 

else {}
}
console . log ( VALUE )

    setTimeout (()=>{ 
        $w ( '#selectionTags1' ). value  = [] 
        $w ( '#selectionTags1' ). value  =  VALUE 
    }, 1 ) 
}) 

});

Thanks :slight_smile:

Are you searching for a “Single-Tag-Selection” ???

If so, take a look onto this one…

The included EXAMPLE will not work anymore (since i have deleted some stuff on my site) but you can read the post, which will show you how to achieve your wished functionality.

I just tried to add the code in my site, it works but the selection remembers the previous tag selection. I saw the reply that you have to use the cose, but how? Thank you for your help!

The code is now the following:
import wixData from ‘wix-data’ ;

const databaseName = ‘Portainnesti’ ;
const databaseField = ‘tipo’ ;

$w . onReady ( function () {

$w ( '#selectionTags1' ). onChange (( event ) => { 
    **const**  selectedTag  =  $w ( '#selectionTags1' ). value ; 
    addItemstoRepeater ( selectedTag ); 
}) 

});

function addItemstoRepeater ( selectedOption = []) {

**let**  dataQuery  =  wixData . query ( databaseName ); 

**if**  ( selectedOption . length  >  0 ) { 
    dataQuery  =  dataQuery . hasSome ( databaseField ,  selectedOption ); 
} 

dataQuery 
    . find () 
    . then ( results  => { 
        **const**  filtereditemsReady  =  results . items ; 
        $w ( '#repeater1' ). data  =  filtereditemsReady ; 
    }) 

}

$w . onReady ( function () {

$w ( '#selectionTags1' ). onChange (()=>{ 

let VALUE = $w ( ‘#selectionTags1’ ). value
let LENGTH = VALUE . length

    console . log ( LENGTH ) 

for ( var i = 0 ; i < LENGTH - 1 ; i ++) {
if ( LENGTH > 1 ) {
VALUE . shift ()

        } 

else { }
}
console . log ( VALUE )

    setTimeout (()=>{ 
        $w ( '#selectionTags1' ). value  = [] 
        $w ( '#selectionTags1' ). value  =  VALUE 
    }, 1 ) 
}) 

});

All you need to do is:

$w('#tags').onChange(event => {
$w('#tags').value = [$w('#tags').value.reverse()[0]];
})

Thank you for your answer :slight_smile:
Where I have to insert it?

@emilianogiovannini0 inside $w.onReady(function () {/HERE/}

@jonatandor35 thank you,
I tried it but it does not work, it just remembers the previous selected tag/tags :frowning:

@emilianogiovannini0 Maybe you have other code on the page that makes the problem.
But the code I posted should work (if you used the correct property IDs, of course).

If you put my code inside $w.onReady and you don’t have other (buggy) code on your page.
You should look at the console for errors.

@jonatandor35 it works only if I first deselect the menu voice and then re-select another one, so I think it resets all the values and starts right.

@emilianogiovannini0 It should take the current value array, reverse its order, take the first item (which is the last to be selected), put it in an empty array and assign it to the tags value.
I don’t know why it doesn’t work for you, but it worked for me when I tried it a long time ago.

Hello,
it’s almost two years that this code is working. Today randomly it just stopped, code is working on the editor but not on the actual public site. Are there any known bugs?

Thanks