dependent dropdown for wordpress

I searched everywhere and can’t find what I’m looking for.. take a look at this code example :

<div class="ccms_form_element cfdiv_custom" id="style_container_div">

<label for="brand">Make:</label>

<select size="1" id="make" class=" validate['required']" title=""  onChange="updatepage();" type="select" name="style">
<option value="-1">–Choose a Make-</option>
<option class="Audio" value="Audi">Audi</option>
<option class="BMW" value="BMW">BMW</option>
</select>
    <div class="clear"></div><div id="error-message-style"></div>

<div id="style-sub-1"  class="BMW"  style="display: none;" name="stylesub1" onchange="ChangeDropdowns(this.value)">
    <label for="brand">Model:</label>

<select name="cat" id="cat" class="postform">
<option value="-1">–Choose a Model-</option>
<option class="level-0" value="172">1 Series</option>
<option class="level-0" value="173">2 Series</option>
<option class="level-0" value="106">3 Series</option>
</select>
</div>
<div id="style-sub-1"  class="Audi"  style="display: none;" name="stylesub1" onchange="ChangeDropdowns(this.value)">
    <label for="brand">Model:</label>

<select name="cat" id="cat" class="postform">
<option value="-1">–Choose a Model-</option>
<option class="level-0" value="169">A1</option>
<option class="level-0" value="170">A3</option>
<option class="level-0" value="171">A4</option>
</select>
</div>
<div class="clear"></div>
<div id="error-message-style-sub-1"></div></div>

<script type="text/javascript">
function updatepage(){
    $("#make").change ( function () {
        var targID  = $(this).val ();
        $("div#style-sub-1").hide ();
        $('.' + targID).show ();
    } )
}
</script>

it’s for wordpress usage , what is wrong? http://jsfiddle.net/sqb4h13r/ I think i had problem with to include Javascript inside a post, I need to combine the call to the script file with the call to the JavaScript itself. but how? can someone guide me?

Related posts

Leave a Reply

1 comment

  1. The Fiddle you link to isn’t working for two reasons:

    1. You never invoke the function updatepage
    2. You haven’t included jQuery on your page

    You can solve this by moving the code out of the function:

    $("#make").change ( function () {
      var targID  = $(this).val ();
      $("div#style-sub-1").hide ();
      $('.' + targID).show ();
    });
    

    and including jQuery.

    Here’s a demo of it working.