I have two or more selects in a form; for simplicity we say two; referring to the first idFilmstarwars I would like to replace idFilmstarwars … id by var selectValue = $ (# idFilmstarwars, ‘option: selected’). val; … with the selectId variable, what can I do?
I’ll explain:
I’m importing a movie list from a file; each select represents a searched movie in the list, therefore in each option there are the different movies of each searched title; it’s all dynamic because I can’t know how many films there will be on the list and how many variations of the film will be found for each film; therefore also the id of the select and of the options is dynamic; each id will have a different value; the only static id is that of the form; my intent once I have imported the movies and put the variants in the options … which I did and it works … and allow the user to choose his variant for each select; therefore I need a system that stores the variation of the select and I thought about onchange; I must therefore memorize this variation; the selectValue variable takes the content of the select through text, however this system works if I indicate in the parameters the id of the select which, being dynamic, changes; what I can’t do is replace the requested parameter as id in a variable that stores in the previous line of the code what is the id of the select; as I wrote it doesn’t work: var selectValue = $ (selectId, ‘option: selected’). text; it gives me an undefined value while if I replace the selectId variable with a static id set by me, it works; I don’t know syntactically what I’m wrong; subsequently the data obtained will be sent to the database
Thank you
<select id="idFilmstarwars " class="form-control">
<option value="0">Select Value 0</option>
<option value="8">Option value 8</option>
<option value="5">Option value 5</option>
<option value="4">Option value 4</option>
</select>
<select id="idFilmmatrix" class="form-control">
<option value="0">Select Value 1</option>
<option value="8">Option value 7</option>
<option value="5">Option value 2</option>
<option value="4">Option value 3</option>
</select>
<input type="text" id="textFieldTextJQ" class="form-control"
placeholder="get value on option select">
$("#idFilmstarwars ").on("change",function(){
//Getting Value
var selectId = $("select").attr("id");
var selectValue = $(selectId, 'option:selected').text;
// var selValue = $("'#' + optionId :selected").text();
alert(selectValue);
var selectValue1 = $(this).children(":selected").text();
alert(selectValue);
//Setting Value
$("#textFieldTextJQ").val(selectValue);
alert(selectValue);
$('#test').attr('id')
});