I am trying to get options with a specific data value out of a variable which I saved all the available options inside.
var optionSaver = $('#directory option'); console.log(optionSaver); $('#projectOrOrganizations').change(function () { var id = $('#projectOrOrganizations').find(':selected').data('contextid'); console.log(id); var options = $('option[data-parentid='+id+']', optionSaver); console.log(options); $('#directory').html(options); }); the optionSaver should persist as long as the the change listener is active
EDIT:
The Html as requsted:
<select name="project" id="projectOrOrganizations"> <option name="organisationOrProjectName_78" data-contextid="78">Organisationsvorlage</option> <option name="organisationOrProjectName_80" data-contextid="80"> Projektvorlage</option> </select> <select name="directory" id="directory"> <option data-parentid="78" value="Verzeichnis">Verzeichnis</option> <option data-parentid="80" value="Verzeichnis">Verzeichnis2</option> </select>
#directory?