How to delete a term entered a form using jQuery?

3

The code below is part of a function that uses a search term in a text field to extract images from the Flickr site. After entering the word to search, it inserts the text below the field, just to show that it is looking for it, but the problem is that whenever I type a new term, it inserts the new term in the page, instead of zeroing the words. images and the term.

var $tag,
    $status,
    $msg;

    if ($("main .container .busca input").val() !== "") {
        $status = $("<p>").text($("main .container .busca input").val());
        $tag = $("main .container .busca input").val();
        $msg = ("<p> Encontramos essas imagens para você:</p>");
        $("main .container .busca").append($msg).append($status);
        $("main .container .busca input").val("");
    };

I would like every time I type a new term, it deletes the previous one, as well as the images, and inserts the new term under the text box. Could someone give me a clue how to do this?

    
asked by anonymous 20.11.2015 / 00:35

1 answer

4

Try this:

var $tag,
$status,$msg;

if ($("main .container .busca input").val() !== "") {
    $("main .container .busca p").text("");
    $status = ("<p>" + $(".busca input").val() + "</p>");
    $tag = $("main .container .busca input").val();
    $msg = ("<p> Encontramos essas imagens para você:</p>");
    $("main .container .busca").append($msg).append($status);
    $("main .container .busca input").val("");
};

or this depending on your HTML code:

var $tag,
$status,$msg;

if ($("main .container .busca input").val() !== "") {
    $("main .container .busca p").remove();
    $status = ("<p>" + $("main .container .busca input").val() + "</p>");
    $tag = $("main .container .busca input").val();
    $msg = ("<p> Encontramos essas imagens para você:</p>");
    $("main .container .busca").append($msg).append($status);
    $("main .container .busca input").val("");
};
    
20.11.2015 / 00:44