Search code examples
javascriptjquerylaravelcombobox

jQuery get selected option from combobox


I have a combobox like this

enter image description here

after that, I have a checkbox. If the checkbox is checked, I want to copy the value of the combobox to an input of text. I write a jQuery code like this to get the value, then I set the value of combobox to an input of text

jQuery to get value of combobox

i check console to see the result. value

I get the value if I check the log. But the value can't set into input text.

value can't set

But, if I make a little change in jQuery to get the key of combobox, the key can be set in the input text.

enter image description here

Then,in the input text I can get the key of combobox

input text

I need help for this.


Solution

  • Your code should work. Please provide the HTML (not the templating code)

    Simplified:

    $("#customCheck1").on("click", function() { 
      const text = this.checked ? $("#province option:selected").text() : ""; 
      $("#current_province").val(text);
    });`
    

    assuming unique IDs