user3822774
user3822774

Reputation: 3

jquery code works on codepen/jsfiddle but not html page

My code for collapsible jquery div works on codepen and JSFiddle, but when I copy everything to my page (content adapted), the div does not expand. What am I missing, or where did I go wrong?

My script tags in the head are as follows:

<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script> 

    <script type="text/javascript">
        $(".header").click(function () {
            $header = $(this);
            $content = $header.next();
            $content.slideToggle(500, function () {
                $header.text(function () {
                    return $content.is(":visible") ? "Collapse" : "Expand";
                });
            });
        });
    </script>
</head>

And my content within the body:

<form method="post" action="sales_report.php" enctype="multipart/form-data">
    //multiple <input /> fields here
    <div class="container">
        <div class="header"><span>+Options</span></div>
        <div class="content">
            <h3>Designate the Column of:</h3>
            <p>Account Name</p>
            <input required type="number" name="sales_report_col_acc" id="sales_report_col_acc" value='1' />
            <p>Sale Amount</p>
            <input required type="number" name="sales_report_col_amt" id="sales_report_col_amt" value='2' />
            <p>Sale Date (optional)</p>
            <input type="number" name="sales_report_col_date" id="sales_report_col_date" value='3' />
            </br /> 
        </div>
    </div>
    <input type="submit" name="sales_report_submit" value="Go" />
</form>

Please advise on how to fix this? Am I missing something crucial that goes along with jquery.

Upvotes: 0

Views: 626

Answers (1)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You need to wrap your code inside ready handler:

$(document).ready(function(){
     //your code here
});

Or,

$(function(){
   //your code here
});

Upvotes: 3

Related Questions