0

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> 
3
  • 1
    And where is your HTML of #directory? Commented Oct 6, 2014 at 8:16
  • So what's the problem..? Commented Oct 6, 2014 at 8:23
  • I cant get the options (optionSaver) which are located in #directory to save permanently. They get overwritten each time the function is called. Commented Oct 6, 2014 at 8:25

2 Answers 2

1

You need to use filter() here because the option element what you are looking for is not descendant of the optionSaver, but it is a member of optionSaver. So

var options = optionSaver.filter('option[data-parentid='+id+']'); 
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to save the variable data you should be using a variable with a greater scope. You define id and options in the scope of the change event. Therefore you should create a variable out of the change event and save it there.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.