how to clear an attribute in html by javascript

Asked

Viewed 165 times

0

I have a problem in my code where it takes a bank value related to the value of the combobox selected by the user and fills in another combobox, until then it is working but when the user gives an onchange again it adds the values instead of overwriting tried to use the reset() and clear() function of jquery did not work follow the code below:

<script>
$(document).ready(function() {
    $("#button").change(function() {
        var valor = $("#button").val();
        alert(valor);
        $.post("procura.php",
            {valor: valor},
                function(data){
                    alert(data);
                    var resultado = data.split(",");
                    for ( var i = 0 ; i < resultado.length ; i++ ){
                        var option = $("<option></option>").appendTo($('#result'));
                        option.attr("value", resultado);
                        option.html(resultado[i]);
                    }
                    document.getElementById("form").reset();
                    });
                });
            });
</script>
</head>
<body>

<select id="button">
    <option value="AVT">AVT</option>
    <option value="MMFT">MMFT</option>
    <option value="RUNIN">RUNIN</option>
</select>
<select id="result" name="result">
    <option value=""></option>
</select>
  • you give me the freedom to edit the title of your question so that it makes more sense? " How to remove options from a select before updating it with others, via Javascript"

2 answers

0

Friend tries to reset the options of your select before the new fill:

$("#button").change(function () {
        var valor = $("#button").val();
        alert(valor);
        $.post("procura.php",
            { valor: valor },
            function (data) {
                alert(data);
                var resultado = data.split(",");
                $('#result').empty();
                for (var i = 0; i < resultado.length; i++) {
                    var option = $("<option></option>").appendTo($('#result'));
                    option.attr("value", resultado);
                    option.html(resultado[i]);
                }
            });
    });

That way whenever any new value comes from the database, it cleans your combobox and adds the values again. I hope I helped friend.

0

Just clear select with id="result".

 $("#result").empty();
 //Aqui segue seu código
 var option = $("<option></option>").appendTo($('#result'));

Browser other questions tagged

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