Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery Toggle Button Trigger

i have this code:

$('.access a').toggle(function() {
    $('link').attr('href', 'styles/accessstyles.css');
    $('body').css('font-size', '16px');
}, function() {
    $('link').attr('href', 'styles/styles.css');
    $('body').css('font-size', text_sizes[text_current_size] + 'px');
});

It works fine. But how would i programmatically trigger the toggle instead of clicking on it?

like image 478
Ozzy Avatar asked Mar 17 '10 21:03

Ozzy


People also ask

How do I toggle a button 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. hide() is run if an element is visible - This creates a toggle effect.

How do I toggle an event in jQuery?

The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on. Note: There is also a jQuery Effects method called toggle().

How do I get the toggle button checked in jQuery?

click(function() { $("input[name=recipients\\[\\]]"). attr('checked', true); }); });

What does it mean by toggle in jQuery?

jQuery | toggle() Method The toggle() method is used to check the visibility of selected elements to toggle between hide() and show() for the selected elements. show() is run when the element is hidden.


1 Answers

Like this:

$('.access a').trigger('click');

Since toggle is just functions that are swapped and executed on a click, that's what you want to trigger. Also available is the shortcut:

$('.access a').click(); //Same as first method
like image 183
Nick Craver Avatar answered Oct 15 '22 01:10

Nick Craver