Insert value in the nearest text input

Asked

Viewed 223 times

-1

good afternoon guys, I’m having a problem in my jquery Here’s the thing, I’ll have a lot of Formulars on one page... Where when typing in each "board" field my jquery inserts a value in the next concession field, from the same form.

I tried using Closest("form"). find("#dealership") but I was unsuccessful

here is a jsfiddle link trying to represent what I want to happen

https://jsfiddle.net/wrh2pszo/1/

  • 1

    Your jsFiddle works if you add jQuery... https://jsfiddle.net/wrh2pszo/3/

  • good night Die, and if I want to add different values (e.g. the value of the board) to each "dealership", as I would?

  • Do you want to use the value you are entering? or do you have specific data for each input?

2 answers

0

I don’t know if it’s the best way, but maybe it’ll solve your problem! https://jsfiddle.net/wrh2pszo/8/

<form>
        <label for="placa">Placa:</label>
        <input type="text" class="placa">

        <label for="concessionaria">Concessionaria:</label>
        <input type="text" name="concessionaria" id="concessionaria">
</form>
<form>
        <label for="placa">Placa:</label>
        <input type="text" class="placa">

        <label for="concessionaria">Concessionaria:</label>
        <input type="text" name="concessionaria" id="concessionaria">
</form>
<form>
        <label for="placa">Placa:</label>
        <input type="text" class="placa">

        <label for="concessionaria">Concessionaria:</label>
        <input type="text" name="concessionaria" id="concessionaria">
</form>

javascript:

$(".placa").on('input', function() {
    $(this).parent().find('#concessionaria').val($(this).val());
});

0

Change the script to:

$(".placa").on('input', function () {
            $(this).parent().find("input[name$='concessionaria']").val($(this).val());
        });

Browser other questions tagged

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