Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Toggle State

Here's the quick and skinny of my issue:

$("a").toggle(function() { /*function A*/ }, function() { /*function B*/ });

Inside function A a form is displayed. If the user successfully completes the form, the form is hidden again (returning to it's original state).

Inside function B the same form is hidden.

The theory behind this is that the user can choose to display the form and fill it out, or they can click again and have the form go back into hiding.

Now my question is this: currently, if the user fills out the form successfully--and it goes into hiding--the user would have to click on the link twice before returning to the toggle state that displays the form.

Is there anyway to programmatically reset the toggle switch to its initial state?

like image 984
neezer Avatar asked Oct 28 '08 18:10

neezer


People also ask

What is toggle () in jQuery?

The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden.

How do you check if toggle is on or off?

settings:id/switch_widget\"]"). getAttribute("Checked"); If the element is disabled then you will see the Result Data as "false" and if it is enabled then you will see the "true." You can use the element identifier: //*[@text="ON"] if there is only one toggle element/switch enabled on the screen.

How do you know if toggle is open?

Simple jQuery code snippets to check if toggle is open or closed. Basically, the current state can be determined by using this test: $(this).is(":hidden"). To see this in action, check out the jQuery.


2 Answers

You can check the state of the toggle in jQuery by using .is(":hidden"). So in basic code what I used:

$("#div_clicked").click(function() {
  if ($("#toggle_div").is(":hidden")) {
     // do this
  } else {
     // do that
}
}); # add missing closing
like image 74
Nicky Vandevoorde Avatar answered Sep 30 '22 17:09

Nicky Vandevoorde


jQuery has two .toggle() methods:

.toggle()

Toggles each of the set of matched elements. If they are shown, toggle makes them hidden. If they are hidden, toggle makes them shown.

.toggle(even, odd)

Toggle between two function calls every other click.

In this case you want the first one. Something like this should do the trick:

$("a").click(function() {
    $("#theForm").toggle();
});
like image 28
foxy Avatar answered Sep 30 '22 17:09

foxy