Identify type of operation with javascript

Asked

Viewed 34 times

1

How can I identify, when the click is "From:" "To:"?

I have a code to add a value from a database field, to another field, follow the example:

HTML:

<div class='row justify-content-center'>
                        <div class="converter-field d-flex flex-column flex-md-row align-items-center mt-3 mb-3">
                            <div class="form-group calc" id="currencyBox1">
                                <span class="calc-symbol-box center" data-target="converter.fromSymbol">Disponível</span>
                                <input type="text" name="balancel_disponivel" id="balance_disponivel" placeholder="Digite o valor" class="form-control valor" />
                            </div>  

                            <i class="fa fa-exchange text-xl text-lg-2xl m-3 currency-swap" onclick="swapCurrencies();return false;"></i> 

                            <div class="form-group calc" id="currencyBox0">
                                <span class="calc-symbol-box center" data-target="converter.toSymbol">Especial</span>
                                <input type="text" name="balance_special" id="balance_especial" placeholder="Digite o valor" class="form-control valor" />
                            </div>
                            <div class="clearfix"></div>
                        </div>

                        <div class="clearfix"></div>
                    </div>

Javascript:

function fillData(a) {
    var d = document.location.href, c = d.match(/\#([A-Z]{3,5})/);
    if (c && c.length > 1) {
        var b = gt("currencyBox1");
        if (!a && b && b.innerHTML.indexOf(c[1]) != -1) {
            swapCurrencies()
        }
    }
    c = d.match(/\#([A-Z]{3,5})=([\-0-9\.eE]+)/);
    if (c && c.length > 2) {
        frm()[c[1]].value = c[2];
        cvrt(frm()[c[1]], !a)
    }
}
function frm() {
    return document.currency.elements
}
function gt(a) {
    return document.getElementById(a)
}
function swapCurrencies() {
    var b = gt("currencyBox0");
    var a = gt("currencyBox1");
    var e = gt("currencyText0");
    var d = gt("currencyText1");
    if (b && a && e && d) {
        var c = b.innerHTML;
        b.innerHTML = a.innerHTML;
        a.innerHTML = c;
        c = e.innerHTML;
        e.innerHTML = d.innerHTML;
        d.innerHTML = c
    }
    fillData(true)
}

I want to pass a parameter to identify, from which field I have to send, to be able to make this exchange of values.

For example, when I click the button to make the field change, I wanted a parameter stating that the first field, is precisely the field that I will be debiting the value, to add in the other.

1 answer

0


I would use a parameter within the same input, something like data-input-target or data-input-swap.

For example:

<input type="text" name="balancel_disponivel" id="balance_disponivel" data-input-swap="balance_especial" placeholder="Digite o valor" class="form-control valor">

[...]

<input type="text" name="balance_special" id="balance_especial" data-input-swap="balance_disponivel" placeholder="Digite o valor" class="form-control valor">

And then via javascript you would take this parameter via getAttribute("data-input-swap") in the element.

Browser other questions tagged

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