How to dynamically append selected option to a div and update if selected option is changed?

OPK

I have my HTML: a <select>, and a <div>:

  <select>
    <option>Please select one</option>
    <option>Volvo</option>
    <option>Saab</option>
    <option>Mercedes</option>
    <option>Audi</option>
  </select>

  <div></div>

When I select an option, I want to append the being selected option value to the <div> element. This in my js code:

    $('select').on('change', function (e){
             $('div').append("//not sure how to write code here");
    });     

What should I add in the append(), so that when a user select an option, it will shows up on my div, if the user change to another option, it will update the content?

For example, if I select Volvo, "Volvo" text will show in my div, if I change to Saab, "Saab" text will show up in my div to replace "Volvo"

leo.fcx

$.append adds new html element inside the target element.

Use instead $.text. It will replace the div content to the new value. Also use $.val to get the current selected value from the select tag. Note that this refers to the select tag

Try this:

 $('select').on('change', function (e){
     $('div').text($(this).val());
 });  

See demo:

$('select').on('change', function(e) {
  $('div').text($(this).val());
});
div {
  border: 1px dashed #999;
  padding: 15px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select>
  <option>Please select one</option>
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</select>

<div>Anything selected yet</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to dynamically change the selected option and update without reloading?

From Dev

Append selected option to FormData

From Dev

Dynamically set option selected

From Dev

Dynamically display selected option

From Dev

Dynamically display selected option

From Dev

How to dynamically set an option selected in PHP

From Dev

angular 6 how to update property if no option is selected

From Dev

Unable to append and update option from select selected jquery

From Dev

How to get selected option

From Dev

(Javascript) How to changed a variable value based on selected option in a dropdown list?

From Dev

(Javascript) How to changed a variable value based on selected option in a dropdown list?

From Dev

Selected option

From Dev

How to get the value of a selected option dynamically and store it into a variable?

From Dev

How to dynamically call data from an API when option is selected

From Dev

Select option selected an not selected

From Dev

Append selected to option after form submission with error

From Dev

How to set the option selected to true if selected

From Dev

appending new option to dropdown list dynamically as selected

From Dev

Struts <html:select> set selected option dynamically

From Dev

Dynamically Change select option when checkboxes are selected

From Dev

How to reselect already selected option

From Dev

How to set the selected option of <select>

From Dev

How to set the selected option of <select>

From Dev

how to change selected option in VueJS?

From Dev

How to set default option selected?

From Dev

How to know the number of selected in option

From Dev

How to update selected option value using Java Script

From Dev

How to make the Selected Option the width of the current Selected Option's text?

From Dev

How to get the selected option value from select inside div

Related Related

  1. 1

    How to dynamically change the selected option and update without reloading?

  2. 2

    Append selected option to FormData

  3. 3

    Dynamically set option selected

  4. 4

    Dynamically display selected option

  5. 5

    Dynamically display selected option

  6. 6

    How to dynamically set an option selected in PHP

  7. 7

    angular 6 how to update property if no option is selected

  8. 8

    Unable to append and update option from select selected jquery

  9. 9

    How to get selected option

  10. 10

    (Javascript) How to changed a variable value based on selected option in a dropdown list?

  11. 11

    (Javascript) How to changed a variable value based on selected option in a dropdown list?

  12. 12

    Selected option

  13. 13

    How to get the value of a selected option dynamically and store it into a variable?

  14. 14

    How to dynamically call data from an API when option is selected

  15. 15

    Select option selected an not selected

  16. 16

    Append selected to option after form submission with error

  17. 17

    How to set the option selected to true if selected

  18. 18

    appending new option to dropdown list dynamically as selected

  19. 19

    Struts <html:select> set selected option dynamically

  20. 20

    Dynamically Change select option when checkboxes are selected

  21. 21

    How to reselect already selected option

  22. 22

    How to set the selected option of <select>

  23. 23

    How to set the selected option of <select>

  24. 24

    how to change selected option in VueJS?

  25. 25

    How to set default option selected?

  26. 26

    How to know the number of selected in option

  27. 27

    How to update selected option value using Java Script

  28. 28

    How to make the Selected Option the width of the current Selected Option's text?

  29. 29

    How to get the selected option value from select inside div

HotTag

Archive