In this whole scenario, I want to do this while selecting 1 option of parent select and child select can show all option which is related with parent select parent and child values are same in HTML code.
$("#ref_type_text").change(function() {
var val = $(this).val();
if (val == "item1") {
$("#ref_type_text_right").html("<option value='test'>item1: test 1</option><option value='test2'>item1: test 2</option>");
} else if (val == "item2") {
$("#ref_type_text_right").html("<option value='test'>item2: test 1</option><option value='test2'>item2: test 2</option>");
} else if (val == "item3") {
$("#ref_type_text_right").html("<option value='test'>item3: test 1</option><option value='test2'>item3: test 2</option>");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>$
<div class="left_pan">$
<label for="ref_type_text">Reference <u>t</u>ype:</label>$
<select name="ref_type_text" id="ref_type_text" class="dropdown" accesskey="t">
<option value="1" selected="selected">Numbered item</option>
<option value="2">Heading</option>
<option value="3">Bookmark</option>
<option value="4">Footnote</option>
</select>
</div>
<div class="right_pan">
<label for="insert_ref_text">Insert <u>r</u>eference to:</label>
<select name="ref_type_text_right" id="ref_type_text_right" class="dropdown" accesskey="t">
<option value="1" selected="selected">Page number</option>
<option value="1">Paragraph number</option>
<option value="1">Paragraph number(no content)</option>
<option value="1">Paragraph number(full content)</option>
<option value="1">Paragraph Text</option>
<option value="1">Above/Below</option>
<option value="2">Heading text</option>
<option value="2">Page number</option>
<option value="2">Heading number</option>
<option value="2">Heading number(no content)</option>
<option value="2">Heading number(full content)</option>
<option value="2">Above/Below</option>
<option value="3">Bookmark text</option>
<option value="3">Page number</option>
<option value="3">Paragraph number</option>
<option value="3">Paragraph number(no content)</option>
<option value="3">Paragraph number(full content)</option>
<option value="3">Above/Below</option>
<option value="4">Footnote number</option>
<option value="4">Page number</option>
<option value="4">Above/Below</option>
<option value="4">Footnote number(formatted)</option>
</select>
</div>
you can just hide all the options in event handler and show the options with matching value, something like this:
$("#ref_type_text").change(function () {
var val = $(this).val();
$("#ref_type_text_right option").hide();
$("#ref_type_text_right option[value=" + val + "]").show();
});
$("#ref_type_text").trigger('change');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="left_pan">
<label for="ref_type_text">Reference <u>t</u>ype:</label>
<select name="ref_type_text" id="ref_type_text" class="dropdown" accesskey="t">
<option value="1" selected="selected">Numbered item</option>
<option value="2">Heading</option>
<option value="3">Bookmark</option>
<option value="4">Footnote</option>
</select>
</div>
<div class="right_pan">
<label for="insert_ref_text">Insert <u>r</u>eference to:</label>
<select name="ref_type_text_right" id="ref_type_text_right" class="dropdown" accesskey="t">
<option value="1" selected="selected">Page number</option>
<option value="1">Paragraph number</option>
<option value="1">Paragraph number(no content)</option>
<option value="1">Paragraph number(full content)</option>
<option value="1">Paragraph Text</option>
<option value="1">Above/Below</option>
<option value="2">Heading text</option>
<option value="2">Page number</option>
<option value="2">Heading number</option>
<option value="2">Heading number(no content)</option>
<option value="2">Heading number(full content)</option>
<option value="2">Above/Below</option>
<option value="3">Bookmark text</option>
<option value="3">Page number</option>
<option value="3">Paragraph number</option>
<option value="3">Paragraph number(no content)</option>
<option value="3">Paragraph number(full content)</option>
<option value="3">Above/Below</option>
<option value="4">Footnote number</option>
<option value="4">Page number</option>
<option value="4">Above/Below</option>
<option value="4">Footnote number(formatted)</option>
</select>
</div>
you can create a function "filter" witch will filter your 2 select with the parameter "data-parent"
$(function(){
// declare the two select
$parentSelect = $("select[name=parentSelect]");
$childSelect = $("select[name=childSelect]");
// select the child items of first parent
filter($parentSelect,$childSelect);
// select the child items of selected parent
$parentSelect.change(function(){
filter($(this),$childSelect);
});
});
function filter(parent,child){
var selectedValue=$(parent).val();
var childSelected = false;
$(child).find('option').each(function() {
if($(this).attr("data-parent") != selectedValue)
$(this).hide();
else{
$(this).show();
if(childSelected === false){
$(this).prop('selected', 'selected');
childSelected = true;
}
}
});
}
select{
width:200px;
font-size: 14px;
padding: 4px 10px;
margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parentItems">
<select name="parentSelect">
<option value="1">Parent 1</option>
<option value="2">Parent 2</option>
<option value="3">Parent 3</option>
</select>
</div>
<div class="ChildItems">
<select name="childSelect">
<option data-parent="1" value="11">child1-1</option>
<option data-parent="1" value="12">child1-2</option>
<option data-parent="1" value="13">child1-3</option>
<option data-parent="2" value="21">child2-1</option>
<option data-parent="2" value="22">child2-2</option>
<option data-parent="2" value="23">child2-3</option>
<option data-parent="3" value="31">child3-1</option>
<option data-parent="3" value="32">child3-2</option>
<option data-parent="3" value="33">child3-3</option>
</select>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With