Questions tagged [toggle]

To toggle a boolean variable is to switch it to its opposite state. If it is on, switch it off; if it is off, switch it on. In a desktop or web user interface, a toggle may be clickable switch or button that alternates the state of something in the application.

To toggle a boolean variable is to switch it to its opposite state. If it is on, switch it off; if it is off, switch it on. In a desktop or web user interface, a toggle may be clickable switch or button that alternates the state of something in the application.

5926 questions
544
votes
9 answers

How to toggle a boolean?

Is there a really easy way to toggle a boolean value in javascript? So far, the best I've got outside of writing a custom function is the ternary: bool = bool ? false : true;
Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
489
votes
31 answers

Is there a better way of writing v = (v == 0 ? 1 : 0);

I want to toggle a variable between 0 and 1. If it's 0 I want to set it to 1, else if it's 1 I want to set it to 0. This is such a fundamental operation that I write so often I'd like to investigate the shortest, clearest possible way of doing it.…
Ollie Glass
  • 19,455
  • 21
  • 76
  • 107
433
votes
24 answers

Toggle Checkboxes on/off

I have the following: $(document).ready(function() { $("#select-all-teammembers").click(function() { $("input[name=recipients\\[\\]]").attr('checked', true); }); }); I'd like the id="select-all-teammembers" when…
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
220
votes
6 answers

Toggle input disabled attribute using jQuery

Here is my code: $("#product1 :checkbox").click(function(){ $(this) .closest('tr') // Find the parent row. .find(":input[type='text']") // Find text elements in that row. …
Tommy Arnold
  • 3,339
  • 8
  • 31
  • 40
171
votes
13 answers

How do I toggle an element's class in pure JavaScript?

I'm looking for a way to convert this jQuery code (which is used in responsive menu section) to pure JavaScript. If it's hard to implement it's OK to use other JavaScript frameworks. $('.btn-navbar').click(function() { …
max imax
  • 2,131
  • 3
  • 15
  • 16
103
votes
8 answers

How to getElementByClass instead of GetElementById with JavaScript?

I'm trying to toggle the visibility of certain DIV elements on a website depending on the class of each DIV. I'm using a basic JavaScript snippet to toggle them. The problem is that the script only uses getElementById, as getElementByClass is not…
Alan
  • 1,033
  • 2
  • 8
  • 4
84
votes
20 answers

How can I trigger an action when a swiftUI toggle() is toggled?

In my SwiftUI view I have to trigger an action when a Toggle() changes its state. The toggle itself only takes a Binding. I therefore tried to trigger the action in the didSet of the @State variable. But the didSet never gets called. Is there any…
Brezentrager
  • 879
  • 1
  • 7
  • 9
83
votes
20 answers

jQuery Toggle Text?

How to toggle HTML text of an anchor tag using jQuery? I want an anchor that when clicked the text alternates between Show Background & Show Text as well as fading in & out another div. This was my best guess: $(function() { …
mtwallet
  • 5,040
  • 14
  • 50
  • 74
81
votes
8 answers

jQuery toggle CSS?

I want to toggle between CSS so when a user clicks the button (#user_button) it shows the menu (#user_options) and changes the CSS, and when the user clicks it again it goes back to normal. So far this is all I have: $('#user_button').click(…
Edd Turtle
  • 1,511
  • 1
  • 13
  • 24
76
votes
12 answers

Set Toggle color in SwiftUI

I've implemented a toggle after following Apple's tutorial on user input. Currently, it looks like this: This is the code that produces this UI: NavigationView { List { Toggle(isOn: $showFavoritesOnly) { Text("Show Favorites…
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
72
votes
5 answers

Does something like jQuery.toggle(boolean) exist?

I write something similar to the following code a lot. It basically toggles an element based on some condition. In the following made-up example, the condition is "If the agree checkbox is checked and the name field isn't…
jessegavin
  • 74,067
  • 28
  • 136
  • 164
71
votes
4 answers

Using jQuery, how do you find only visible elements and leave hidden elements alone?

So I start with items 1-4:
Lorem
o_O
  • 5,527
  • 12
  • 52
  • 90
71
votes
10 answers

Button text toggle in jquery

When i click ".pushme" button, it turns its text to "Don't push me". I want to turn the text again to "push me" when button is clicked again. How can i do that?
Dorukcan Kişin
  • 1,121
  • 2
  • 14
  • 29
70
votes
15 answers

Click toggle with jQuery

I've used a hover function where you do x on mouseover and y and mouseout. I'm trying the same for click but it doesn't seem to work: $('.offer').click(function(){ $(this).find(':checkbox').attr('checked', true ); },function(){ …
eozzy
  • 66,048
  • 104
  • 272
  • 428
68
votes
9 answers

bootstrap initially collapsed element

I am using the bootstrap template and I would like to change the way the accordion works by default. How can I get the toggle to be closed when page is first seen (upon load)?
JoshuaJeanThree
  • 1,382
  • 2
  • 22
  • 41
1
2 3
99 100