1

I need to call the jQuery function below without a button click. How to do that?

jQuery:

  <script>
  $("#nav").click(function(e) {
        e.preventDefault();
        $("#nav").toggleClass("active");
    });
    </script>

In pure JavaScript I would do it like the function below, but how to do it in jQuery?

 function toggleWindow(){
    //alert("OK");
    }
1
  • in pure javascript also to call function we have to add event Commented Apr 19, 2014 at 12:26

2 Answers 2

3

Remove the .click() and put it inside a self executing function:

(function(){
   $("#nav").toggleClass("active");
})();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but how to give the function a name? Is this correct?: (function toggleWindow(){
0

just dont include click() event

 <script>
 $(document).ready(function(){
        $("#nav").toggleClass("active");
 });

 </script>

Here , there is no need of event .As soon as DOM HTML elements are loaded , the script will be executed.

working fiddle [FIDDLE]( http://jsfiddle.net/Bqn5H/1/)

2 Comments

It's not working for the template I'm using: startbootstrap.com/templates/simple-sidebar.html I added your code but the sidebar stays and doesn't toogle on document ready. Any ideas?
zBut fiddle working there should not be any problem. give me link to ur website to test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.