Add fields on page as selected option in a select options menu

Asked

Viewed 14 times

1

I am creating a form and I want that as the option selected in the menu a different html code is shown on the screen. I’m using jquery to add the new fields. But each option selected the new fields are concatenated, that is, each time an option is selected a new value is shown, I wanted it to be shown only once. Follows the code.

$(document).ready(function() {
$( ".form-control" ).change(function(){
    var varURL = $("option:selected", this).val();
    $(".form-group").append('O valor selecionado é: ' + varURL);
});
});
  • 1

    exchange the append for html

  • 1

    I already tried, but the content is replaced and the options menu disappears.

  • 1

    then create a div inside that form-group just to get the html()

  • so it doesn’t delete the menu

  • 1

    I did it and it worked!

1 answer

1


Create a new div inside the form-group, to receive the values, then using html() you add the values in this div:

$(document).ready(function() {
    $( ".form-control" ).change(function(){
        var varURL = $("option:selected", this).val();
        $("ID_DA_NOVA_DIV").html('O valor selecionado é: ' + varURL);
    });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.