1

How would I write the following code to jQuery?

       $("select").change(function(){
    if($("#selectedid").is(":selected")){
        $("#showblock").slideDown("slow");
    } else { $("#showblock").slideUp("slow"); } 
});

I also tried the following:

jQuery("select").change(function($){
        if($("#selectedid").is(":selected")){
            $("#showblock").slideDown("slow");
        } else { $("#showblock").slideUp("slow"); } 
    });

It's for wordpress. Thanks!

Trekdrop
  • 475
  • 7
  • 27

3 Answers3

2

In wordpress you'll probably get errors because wordpress includes a version of jquery that is editted to not use the '$' operator

you have to replace every instance of the '$' with jQuery so your code should look like this;

jQuery("select").change(function(){
    if(jQuery("#selectedid").is(":selected")){
        jQuery("#showblock").slideDown("slow");
    } else { 
        jQuery("#showblock").slideUp("slow"); 
    } 
});

try that and tell me how it works for you.

an easier way would be just to include a standard (downloaded) version of jquery and include it in your header.php

pythonian29033
  • 5,148
  • 5
  • 31
  • 56
1

I think it happening because You have to resolve the conflict for "$"

From Word Press Codex

jQuery(document).ready(function($) {
    // Inside of this function, $() will work as an alias for jQuery()
    // and other libraries also using $ will not be accessible under this shortcut
});
Anand
  • 14,545
  • 8
  • 32
  • 44
0

You need to just wrap your code into immediately invoking function and pass Jquery to it.

(function($) {

    $("select").change(function(){
       if($("#selectedid").is(":selected")){
           $("#showblock").slideDown("slow");
       } else { 
           $("#showblock").slideUp("slow");
       } 
  });

})(jQuery);
patelarpan
  • 7,188
  • 2
  • 20
  • 25