acctman
acctman

Reputation: 4349

Code not working when document.ready is added

the code below works fine on jsFiddle without $(document).ready(function() { and the closing brackets. (ex: http://jsfiddle.net/cEDYD/ ) but soon as I put the code on my webpage with the document ready it stops works. Am I missing something? I've included jquery

<script type="text/javascript">
$(document).ready(function() {
    function showDiv(element, pro2) {
        if (pro2.children("option:selected").val() == "cpl") element.show();
        else element.hide();
    }

    var myElement = $("div#pro2");
    var mypro2 = $("select#ptype");

    $("select").change(function() {
        showDiv(myElement, mypro2)
    });
});
</script>

Upvotes: 0

Views: 154

Answers (3)

jfriend00
jfriend00

Reputation: 707218

Your jsfiddle has a setting that automatically wraps the code in document.ready(). That's why it works without the ready() in the jsFiddle (see where is says onload in the upper left panel).

document.ready() is needed in a regular web page unless you place the javascript after the body HTML.

Upvotes: 0

acctman
acctman

Reputation: 4349

problem ended up being a mistype character in my css file. -thanks for the testing guys

Upvotes: 0

westo
westo

Reputation: 575

Is there a reason you wouldn't want this wrapped in a document ready handler?

Appears the DOM is not ready at the time your javascript is executing.

Check the javascript console. Google Chrome is excellent for JS debugging.

Upvotes: 1

Related Questions