wazzaday
wazzaday

Reputation: 9664

how to trigger javascript when a dropdown option is selected

I have two dropdown boxes for car makes and models, when a make is selected i need all its models to show in the next box. As of now using the following code the models show up when the dropdown is clicked.

    window.onmousedown = function(e){
    this.id = e.target.id;
    if(this.id == "vehicle_makes"){
        var make = document.getElementById("vehicle_makes").value;
        ajaxCall(make);
    }
}

However i need the javascript to trigger when an option from the dropdown is selected rather than when the drop down is opened.

here also is the html - with some php

<div class="vehicle-search-wrap">
    <form method="get" id="searchform" action="<?php bloginfo('url'); ?>">
        <div>
            <select id="vehicle_makes" name="s">
            <?php
                foreach($makes as $make){ //put all makes in dropdown
                    echo "<option value='". $make ."'>". $make ."</option>";
                }
            ?>
            </select>
            <select id="model_drop" name="vmodels">
            <?php
            //nothing to start
            ?>
            </select>       
            <input type="submit" value="Search Vehicle" />
        </div>
    </form>
</div>

Upvotes: 2

Views: 3275

Answers (2)

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Try this:

<select name="Pizza" size="5"
    onchange="myFunctionHere()">
  <option value="P101">Pizza Napoli</option>
  <option value="P102">Pizza Roma</option>
</select>

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

use the onchange event of selectbox.

here is a demo

Upvotes: 6

Related Questions