0

My Producer model has Id and Name

Movie model has Title and Producer as property.

My index.html :

<select id="selectMovieProducer">
                        
</select>

I have following event

$("body").ready(function(){
    var requestUrl = host + port + moviesEndPoint;
    $.getJSON(requestUrl, setMovies)
});

setMovies function from the code above generates (appends) a table with all movies, and then a <option> element for each Producer (appends to <Select> from HTML). Each <option> has value property set to Producer's Id while displaying its Name (in a dropdown/select).

Im working on this event to POST new movie:

 $("#movieForm").submit(function(e){
        e.preventDefault();
    
        var title = $("#movieTitle").val();
        var genre = $("#movieGenre").val();
        var premierYear = $("#moviePremierYear").val();
        var producerId = ???    
});

How do i get/access value property of the selected <option> ? I need it so i can create a movie object and send it to my post method.

Alex
  • 21
  • 10
  • https://stackoverflow.com/a/13089959/8177490 Did you try this to get the selected value of the option? – digitalsuite.net Nov 22 '20 at 23:07
  • 1
    @digitalsuite.net I was not aware of this. His 'recommended' way of selecting is too confusing. I will use his alternative $("option:selected").val(); Thank you! – Alex Nov 22 '20 at 23:27

1 Answers1

0

https://stackoverflow.com/a/13089959/8177490

or

var producerId = $("option:selected").val();
Alex
  • 21
  • 10