Targeting one specific value based off index

I’m using jQuery to populate a standard HTML select list from a JSON file. The populate function works fine, but now I need to target a specific value of whatever dropdown is selected to get a value. The .0xx AFM is what goes in the dropdown box, the value is the value that’s assigned to the select element, and the price is the price associated with that dropdown. What I’m trying to achieve is use jQuery or JS to get the price associated with the selected dropdown. That way I can use the price value as a variable in a calculation formula.

My JSON file Populate JSON code Drop down

Professor Asked on October 26, 2020 in Android langue.
Add Comment
1 Answer(s)

    You can find the price each time a value is selected from the list. I can’t edit your code because it is a screenshot, but this would go INSIDE the get getJSON call but AFTER the each loop.

    Something like:

    // inside your getJson call, after your $.each loop    $(dropdown).on('change', function() {    const selectedValue = $(this).val();    const match = Object.values(data).find(entry => entry.value === selectedValue);    console.log(match && match.price);  });

    Professor Answered on October 26, 2020.
    Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.