OPK OPK - 2 years ago 103
HTML Question

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

I have my HTML: a

, and a

<option>Please select one</option>


When I select an option, I want to append the being selected option value to the
This in my

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

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

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

Answer Source

$.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){

See JSFiddle demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download