Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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
});

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top