Anthony
Anthony

Reputation: 4050

jQuery accordion() not working

My jQuery accordion() is not working on my HTML paragraphs. Where did I go wrong?

simple.html

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <div id="mainContent">
            <h1>This is an According Example</h1>           
        </div>
        <div id="accordion">
            <h3><a href="#">Heading for first sentence</a></h3>
            <div>
            <p>This is the first sentence.</p>
            </div>          
            <h3><a href="#">Heading for second sentence</a></h3>            
            <div>
            <p>This is the second sentence.</p>
            </div>
            <h3><a href="#">Heading for third sentence</a></h3>
            <div>
            <p>This is the third sentence.</p>
            </div>
        </div>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
        <script src="myscript.js"></script>
    </body>
</html>

myscript.js

window.onload = function(){
    $("#accordion").accordion();
};

Upvotes: 1

Views: 24432

Answers (3)

Thomas Buckley
Thomas Buckley

Reputation: 6046

$(document).ready(function() {
      $("#accordion").accordion();
});

Upvotes: 0

MG_Bautista
MG_Bautista

Reputation: 2663

Use this...

<!DOCTYPE html>
<html>
    <head>
    <title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
    <script src="myscript.js"></script>
</head>
<body>
    <div id="mainContent">
        <h1>This is an According Example</h1>           
    </div>
    <div id="accordion">
        <h3><a href="#">Heading for first sentence</a></h3>
        <div>
        <p>This is the first sentence.</p>
        </div>          
        <h3><a href="#">Heading for second sentence</a></h3>            
        <div>
        <p>This is the second sentence.</p>
        </div>
        <h3><a href="#">Heading for third sentence</a></h3>
        <div>
        <p>This is the third sentence.</p>
        </div>
    </div>
</body>

And put this in the head tag

<script>
    $(document).on('ready', function(){
        $("#accordion").accordion();
    });
</script>

And see this jsFiddle example

Upvotes: 2

Mooseman
Mooseman

Reputation: 18891

Two problems: You are not including the jQuery UI CSS, also your scripts must be included your <head>. Use this:

<head>
  <title>My Title</title>
  <link href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css">
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <script>$(document).ready(function(){ $( "#accordion" ).accordion(); });</script>
</head>

and remove the scripts from the bottom of your page.

Fiddle: http://jsfiddle.net/cxJW6/ (This doesn't mean much because your problem was with including jQuery+jQuery UI in your page)

Upvotes: 2

Related Questions