-1

I'm getting the following error when using the simple code within wordpress. any idea how to solve this problem?

error:

Uncaught TypeError: Property '$' of object [object Object] is not a function 
(anonymous function) 
x.event.dispatch
v.handle

Javascript:

<script type="text/javascript">
jQuery('#room_type').change(function(){
        var location = $(this).val(),
            div = $('#' + location);
        jQuery('div').hide();
        div.show();
});
</script>

HTML:

<select id="room_type">
    <option value="">-- Type --</option>
    <option value="oneroom">One room</option>
    <option value="tworoom">Two room</option>
</select>

<div id="oneroom" style="display:none">
    CONTENT 1
</div>
<div id="tworoom" style="display:none">
    CONTENT 2
</div>
Jason P
  • 26,984
  • 3
  • 31
  • 45
acctman
  • 4,229
  • 30
  • 98
  • 142

5 Answers5

3

Wrap your functions and jQuery code in this:

;(function ($) {

    //Code in here

})(jQuery);

This assigns the $ to jQuery only. Doing this prevents conflicts with other libraries/code that also use the $. Also leading this code with a ; protects your function from unclosed scripts. I wouldn't rely on the noConflict() method.

<script type="text/javascript">
;(function ($) {
    $('#room_type').change(function(){
        var location = $(this).val(),
            div = $('#' + location);
        $('div').hide();
        div.show();
    });
})(jQuery);
</script>

jsFiddle

For you other problem, try this:

<script type="text/javascript">
;(function ($) {
    $('#room_type').change(function(){
        var location = $(this).val(),
            div = $('#' + location);
        $("div[id$='room']").hide();
        div.show();
    });
})(jQuery);
</script>
Jonny Sooter
  • 2,417
  • 1
  • 24
  • 40
3

That is because $ is not defined in your code (jQuery.noConflict was probably used somewhere)

Just use jQuery as your jQuery variable

Naftali
  • 144,921
  • 39
  • 244
  • 303
2

jQuery.noConflict was probably called, so you won't be able to use the shorthand $ without first setting up an alias.

This is often done with:

(function ($) {//jQuery is now stored as the `$` parameter in this function
    ...code here...
}(jQuery));

Alternatively, for document.ready you can use the aliasing shorthand:

jQuery(function ($) {
   //this code will execute on DOM ready and the `$` parameter will be set
    ...code here...
});

Alternatively, stop using $(...) as a function, and replace it with the more verbose jQuery(...):

jQuery('#room_type').change(function(){
    var location = jQuery(this).val(),
        div = jQuery('#' + location);
    jQuery('div').hide();
    div.show();
});
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • the page is being refreshed to a blank window every time I select an option from the drop down. no error in the console window – acctman Aug 29 '13 at 20:29
  • 1
    @acctman, that would be because you are hiding every div on the page with `jQuery('div').hide()`. – zzzzBov Aug 29 '13 at 20:35
  • how do i target just id="oneroom" and id="tworoom" – acctman Aug 29 '13 at 20:50
  • @acctman, I recommend googling that question first, and if you can't find an answer, to then move on to asking a question on [SO]. [That question is so laughably easy to answer, that it concerns me that you're not able to find the answer on your own](http://api.jquery.com/id-selector/). – zzzzBov Aug 29 '13 at 21:10
1

Do what Jonny Sooter said, or change any $ to jQuery like so:

<script type="text/javascript">
jQuery('#room_type').change(function(){
    var location = jQuery(this).val(),
        div = jQuery('#' + location);
    jQuery('div').hide();
    div.show();
});
</script>
Kevin Jantzer
  • 9,215
  • 2
  • 28
  • 52
1
<script type="text/javascript">
jQuery('#room_type').change(function(){
        var location = $(this).val(),
            div = $('#' + location);
        jQuery('div').hide();
        div.show();
});
</script>

change this to:

<script type="text/javascript">
jQuery('#room_type').change(function(){
        var location = jQuery(this).val(),
            div = jQuery('#' + location);
        jQuery('div').hide();
        div.show();
});
</script>
Kevin Jantzer
  • 9,215
  • 2
  • 28
  • 52
Pranay Bhardwaj
  • 1,059
  • 8
  • 9