How do I delete previous value using javasript

1

I'm trying to develop an information sellection, where in the end it will form the product code, as shown in the image below.

Butbychoosingthefieldsitadds,butifyouneedtochange,donotdeletetheoldoneitstaysthenewoneisinserted,andsoon.

htmlcode

<div>G<pid="teste"></p></div>

javascript code

$(function(){
$("#cidades").change(function(){
            $('#colors div').hide();
            $('#'+$(this).val()).show();
        });
$("#estados").change(function(){
var id = $(this).val();
    $.ajax({
        type: "POST",
        url:"../../paginas/pesquisa/pesquisa_codigo_giga.php?id="+id,
        dataType:"text",
        success:function(res){
            $("#cidades").children(".cidades").remove();
            $("#cidades").append(res);
        }
    });
});
$("#cidades").change(function(){
var id = $(this).val();
    $.ajax({
        type: "POST",
        url:"../../paginas/pesquisa/pesquisa_codigo_giga_ind_out.php?id="+id,
        dataType:"text",
        success:function(res){
            $("#teste").children(".teste").remove();
            $("#teste").append(res);
        }
    });
});});

php code

<?php
include "../../controle/conexao.php";
$id = $_GET['id'];
$sql = "SELECT * FROM tabela_indoor_outdoor WHERE tabela_indoor_outdoor_id='$id'";
$sql = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($sql)){
$nome = $row['tabela_ind_out_cod_giga'];
echo $nome;
}
?>

I need to select the code in sequence, it will return the values, and if the person changes it, delete the old code and insert the new code.

    
asked by anonymous 11.11.2016 / 16:38

2 answers

1

You can simply clean up using .empty()

Instead of using $("#cidades").children(".cidades").remove(); use:

$("#teste").empty();

In this way it will remove all elements within its element <p id="teste"></p>

or .text();

 $("#teste").text("");

What works to replace existing text within your element with "" ;

    
11.11.2016 / 16:48
1

In cidades you do not need to bind two events .change , and then to replace the values just change the .append method to .html :

$(function(){
    $("#estados").change(function(){
    var id = $(this).val();
        $.ajax({
            type: "POST",
            url:"../../paginas/pesquisa/pesquisa_codigo_giga.php?id="+id,
            dataType:"text",
            success:function(res){
                $("#cidades").html(res);
            }
        });
    });
    $("#cidades").change(function(){

        $('#colors div').hide();
        $('#'+$(this).val()).show();

        var id = $(this).val();
        $.ajax({
            type: "POST",
            url:"../../paginas/pesquisa/pesquisa_codigo_giga_ind_out.php?id="+id,
            dataType:"text",
            success:function(res){
                $("#teste").html(res);
            }
    });
});
});
    
11.11.2016 / 16:47