Find element to remove via jQuery

Asked

Viewed 573 times

1

I have a routine in JS which is as follows:

$('body').on('click', 'a.remover', function(e) {
    e.preventDefault();
    $(this).closest('.info').html("<p>Escolha ao lado</p>").parent().find('input[type=hidden]').val();
});

I would like you to click on . remove and look for the div. closer info, replace its contents with the above, and also clean the value of two hiddens input that are in the Rent of . info...

But did not. Did not return any syntax error, but did not execute the function.

HTML Structure (Debugger Print, since elements are added dynamically):

inserir a descrição da imagem aqui

How can I proceed?

  • Add html to the question so it’s easy to reproduce the problem and give an answer working.

1 answer

1


Your code is almost correct, missing only add two quotes within .val() to clean up the inputs (I’ll leave the inputs visible in the sandbox to see the effect):

$('body').on('click', 'a.remover', function(e) {
    e.preventDefault();
    $(this).closest('.info').html("<p>Escolha ao lado</p>").parent().find('input[type=hidden]').val('');
});

$('body').on('click', 'a.remover', function(e) {
    e.preventDefault();
    $(this).closest('.info').html("<p>Escolha ao lado</p>").parent().find('input[type=text]').val('');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="carrinhoPacoteSigame">
    <div class="info">
        <table class="table">
            <tr>
                <td>
                    <a href="#" class="remover">Remover Item</a>
                </td>
            </tr>
        </table>
    </div>
        <input type="text" name="hdProduto03" value="19" />
        <input type="text" name="hdValor03" value="30" />
</div>

  • Perfect! I forgot the damn.

Browser other questions tagged

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