Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

dependent drop down list in jquery

$(function(){

  // add a supercategory for the processor
  var $supcat = $("#processor"),
      $cat = $("#motherboard"),
      $subcat = $(".subcat");
  
  // duplicate the code for the processor -> motherboard
  $supcat.on("change",function(){
    var _rel = $(this).val();
    $cat.find("option").attr("style","");
    $cat.val("");
    if(!_rel) return $cat.prop("disabled",true);
    $cat.find("[rel~='"+_rel+"']").show();
    $cat.prop("disabled",false);
  });
  
  $cat.on("change",function(){
    var _rel = $(this).val();
    $subcat.find("option").attr("style","");
    $subcat.val("");
    if(!_rel) return $subcat.prop("disabled",true);
    $subcat.find("[rel~='"+_rel+"']").show();
    $subcat.prop("disabled",false);
  });
});

//////////////////////////////////////////////
.subcat option{
  display:none;
}

.subcat option.label{
  display:block;
}

/* useless template */

div{
  margin-bottom:10px;
  float:left;
  width:100%;
}
label{
  float:left;
  width:200px;
  margin-left:20px
}
p{
  padding:5px 20px;
  background-color:#f1f1f1;
  float:left;
  border:solid 1px #ccc;
  color:#333;
  text-transform:uppercase;
  margin: 20px 0 30px;
}
body{
  font-family:arial;
  font-size:14px;
}

///////////////////////////////////////////////

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h1>Made your PC:</h1>

<div>
  <label>Processeor: </label><select id="processor" name="processor">
  <option class="label" value>Select Processor</option>
  <!-- Home Ware -->
  <option value="P1">Processor 1</option>
  <option value="P2">Processor 2</option>
  <option value="P3">Processor 3</option>
  <option value="P4">Processor 4</option>

  </select>
</div>

<p><strong>Only compatible components will show.</strong></p>
<div>
  <label>Select motherboard: </label><select id="motherboard" name="motherboard" class="subcat" disabled="disabled">
  <option class="label" value>Select Motherboard</option>
  <!-- Home Ware -->
  <option rel="P1 P2" value="AS1">ASUS RAMPAGE V EXTREME</option>
  <option rel="P2 P3" value="AS2">ASUS ATX DDR3 2600 LGA</option>
  <option rel="P1 P3 P4" value="GB1">Gigabyte AM3+</option>
  <option rel="P2 P4" value="MSI1">MSI ATX DDR3 2600 LGA 1150</option>

  </select>
</div>
<div>
  <label>Select RAM: </label> <select disabled="disabled" class="subcat" id="RAM" name="RAM">
  <option class="label" value>RAM Memory</option>
  <option rel="AS1 AS2 GB1" value="KI1">Kingston Value RAM</option>
  <option rel="AS1 AS2 MSI1" value="P5KPL">P5KPL-AM SE</option>
  <option rel="MSI1 GB1" value="960GM">960GM-VGS3 FX </option>
  </select>
</div>

<div>
  <label>Select Video Board: </label> <select disabled="disabled" class="subcat" id="video-card" name="video-card">
  <option class="label" value>Video Card</option>
  <option rel="MSI1 AS2" value="EVGA8400">EVGA GeForce 8400 GS</option>
  <option rel="AS1" value="XFXAMD">XFX AMD Radeon HD 5450</option>
  <option rel="MSI1 GB1" value="GTX750Ti">EVGA GeForce GTX 750Ti SC</option>
  </select>
</div>

///////////////////////////////////////////

function updateSelects(obj1, obj2) {
    var _rel = $(obj1).val();
    obj2.find("option").attr("style","");
    obj2.val("");
    if(!_rel) return obj2.prop("disabled",true);
    obj2.find("[rel~='"+_rel+"']").show();
    obj2.prop("disabled",false);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how do i set CORS policy for nodejs sever 
Javascript :: Cannot redefine property: clientWidth 
Javascript :: How to pass variables from one page to another with AngularJS 
Javascript :: Setting the default value in the drop down list in AngularJS 
Javascript :: angularjs How to set code view as deafult instead of tree in jsoneditor 
Javascript :: AngularJs: Display HTML elements from string variable in JSP page 
Javascript :: how to replace img url using jquery on mobile screen 
Javascript :: Popover AngularJs quickly disappearing 
Javascript :: Angular Frontend - How do I change a value I got from backend in frontend 
Javascript :: Se Chartjs horizontal 
Javascript :: Conditional navigation inside Tabs 
Javascript :: When doing a booking system, where would it be better to do? Back end or front end 
Javascript :: wrapping a span tag with an a tag with a href target same as the text of the span 
Javascript :: python regex consecutive characters 
Javascript :: PAN SNAP 
Javascript :: Using javascript code in Jade views - if(variable) shows undefined instead of passing 
Javascript :: aws amplify react site is blank after updating react-router-dom 
Javascript :: cleave js 
Javascript :: input json decode 
Javascript :: bullet mechanism in phaser 
Javascript :: function listview list grud abnAlhaj 
Javascript :: force browser reload page from server javascript 
Javascript :: Fibonacci numbers for n terms 
Javascript :: board in javascript 
Javascript :: convert .js to .ts 
Javascript :: Dependency Injection in Node.js 
Javascript :: docker healthcheck express 
Javascript :: mongodb instructions 
Javascript :: toISOString() in electron 
Javascript :: ES6 reactjs problems 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =