Join the Webstudio community

Updated 3 months ago

Anyone can share a tutorial how to use switch component?

At a glance

The post is about a community member who wants to include a switch above their pricing cards to toggle between monthly and annual pricing. The comments provide a suggested workaround, where the community member puts the switch inside a form, adds a button to the form, and uses JavaScript to click the button when the switch is toggled. The key steps include setting the form method to GET, leaving the action blank, and using the switch's name to conditionally render content. While there is no explicitly marked answer, the community members provide a detailed solution to the problem.

Useful resources
I have a pricing table on my website. Want to include a switch above the pricing cards to toggle between monthly and annual pricing. I have zero idea how to affect the cards when the switch status is changed. Any help is appreciated
Attachment
Screenshot_2024-10-19_at_5.16.18_PM.png
A
B
R
13 comments
I'm working on the same thing and just configured things a few hours ago.
Since WebStudio doesn't support global data variables yet, @TrySound was very kind to suggest a workaround here: https://discord.com/channels/955905230107738152/1297247623962689616

Here's how I configured things:
First, I put the switch inside a form, and added a button to the form as well

(for some reason a document.getElementById('myForm').submit(); was throwing an error for me)

Also, to the Switch, I added a child HTML Embed
Attachment
image.png
I set the form Method to GET .

Edit: Leaving the Action blank works
Attachment
image.png
Try to leave action empty. It should use current page then
Ah, didn't work?
Where did it redirect?
Next I made sure my button was set to submit, and gave it an ID.
Attachment
image.png
Now, I couldn't programatically reference the button anymore if I disabled the show, so I set a display: none; on the button
Attachment
image.png
Next, I added the following code to the expression editor (not the HTML code directly).
Plain Text
`<script> 
console.log(${switch}) // Just for testing
document.getElementById('pricing-form-button').click()
</script>` 
Attachment
image.png
This clicks my button everytime I toggle the switch ๐ŸŽ‰
Last, make sure your switch has a name
That's what shows up in the url parameters
Attachment
image.png
Now you can use the name you just defined to conditionally render stuff in a collection or any other element
Attachment
image.png
Just checked this, removed the Action, it still works!
Editing my previous comment.
Thanks for sharing this. Iโ€™ll give it a whirl and see how far I get. I really appreciate it!
Add a reply
Sign up and join the conversation on Discord
`","dateCreated":"2024-10-19T22:05:56.331Z","dateModified":"2024-10-19T22:05:56.331Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/3e1fcfa0-e900-4d79-a1f6-dee2844ab775","name":"Ash","identifier":"3e1fcfa0-e900-4d79-a1f6-dee2844ab775","image":"https://cdn.discordapp.com/embed/avatars/4.png"},"commentCount":0,"comment":[],"position":8,"upvoteCount":0},{"@type":"Comment","text":"This clicks my button everytime I toggle the switch ๐ŸŽ‰","dateCreated":"2024-10-19T22:06:29.447Z","dateModified":"2024-10-19T22:06:29.447Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/3e1fcfa0-e900-4d79-a1f6-dee2844ab775","name":"Ash","identifier":"3e1fcfa0-e900-4d79-a1f6-dee2844ab775","image":"https://cdn.discordapp.com/embed/avatars/4.png"},"commentCount":0,"comment":[],"position":9,"upvoteCount":0},{"@type":"Comment","text":"Last, make sure your switch has a nameThat's what shows up in the url parameters","dateCreated":"2024-10-19T22:07:22.923Z","dateModified":"2024-10-19T22:07:22.923Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/3e1fcfa0-e900-4d79-a1f6-dee2844ab775","name":"Ash","identifier":"3e1fcfa0-e900-4d79-a1f6-dee2844ab775","image":"https://cdn.discordapp.com/embed/avatars/4.png"},"commentCount":0,"comment":[],"position":10,"upvoteCount":0},{"@type":"Comment","text":"Now you can use the name you just defined to conditionally render stuff in a collection or any other element","dateCreated":"2024-10-19T22:08:42.890Z","dateModified":"2024-10-19T22:08:42.890Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/3e1fcfa0-e900-4d79-a1f6-dee2844ab775","name":"Ash","identifier":"3e1fcfa0-e900-4d79-a1f6-dee2844ab775","image":"https://cdn.discordapp.com/embed/avatars/4.png"},"commentCount":0,"comment":[],"position":11,"upvoteCount":0},{"@type":"Comment","text":"Just checked this, removed the Action, it still works!Editing my previous comment.","dateCreated":"2024-10-19T22:12:13.132Z","dateModified":"2024-10-19T22:12:13.132Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/3e1fcfa0-e900-4d79-a1f6-dee2844ab775","name":"Ash","identifier":"3e1fcfa0-e900-4d79-a1f6-dee2844ab775","image":"https://cdn.discordapp.com/embed/avatars/4.png"},"commentCount":0,"comment":[],"position":12,"upvoteCount":0},{"@type":"Comment","text":"Thanks for sharing this. Iโ€™ll give it a whirl and see how far I get. I really appreciate it!","dateCreated":"2024-10-20T00:46:14.676Z","dateModified":"2024-10-20T00:46:14.676Z","author":{"@type":"Person","url":"https://help.webstudio.is/members/af3300e8-6e16-44fe-9556-eb1ce10e3a62","name":"RichardKistnen","identifier":"af3300e8-6e16-44fe-9556-eb1ce10e3a62","image":"https://cdn.discordapp.com/avatars/1184553690204475392/c9c27bd404fbcb5e1efc8c2b0de65ac3.webp?size=256"},"commentCount":0,"comment":[],"position":13,"upvoteCount":0}],"author":{"@type":"Person","url":"https://help.webstudio.is/members/af3300e8-6e16-44fe-9556-eb1ce10e3a62","name":"RichardKistnen","identifier":"af3300e8-6e16-44fe-9556-eb1ce10e3a62","image":"https://cdn.discordapp.com/avatars/1184553690204475392/c9c27bd404fbcb5e1efc8c2b0de65ac3.webp?size=256"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0}}]