Skip to content

Instantly share code, notes, and snippets.

@ncimino
Created October 13, 2012 16:34
Show Gist options
  • Select an option

  • Save ncimino/3885267 to your computer and use it in GitHub Desktop.

Select an option

Save ncimino/3885267 to your computer and use it in GitHub Desktop.
jQuery create drop-downs and auto-filter
$(function() {
$("input[type=button][value=Add]").click(function(e) {
for (i = 0; i < document.getElementById('sel').value; i++) {
e.preventDefault();
var j = 1;
var newDiv = $("<div>").appendTo("#dropbox");
$("<select>").attr("name", "input1_"+j).appendTo(newDiv).append(
$("<option>").val("0").text("Option 1"), $("<option>").val("1").text("Option 2"));
$("<select>").attr("name", "input2_"+j).appendTo(newDiv).append(
$("<option>").val("0").text("Option 1"), $("<option>").val("1").text("Option 2"));
$("<select>").attr("name", "input3_"+j).appendTo(newDiv).append(
$("<option>").val("0").text("Option 1"), $("<option>").val("1").text("Option 2"));
$("<input>").attr("name", "input4_"+j).appendTo(newDiv);
$("<button>").text("Remove").appendTo(newDiv).click(function(e) {
e.preventDefault();
$(this).parent().remove();
})
j++;
}
})
})
$(function() {
$("select[id!=sel]").live('change',function(e) {
selectedValue = $(this).find(":selected").val()
if ($(this).next().attr('name').match(/input[0-9]_[0-9]/)) {
$(this).next().find('option').each(function() {
if ($(this).val() == selectedValue) {
$(this).hide();
} else {
$(this).show();
}
})
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment