Search code examples
javascriptjqueryhtmldom-manipulation

JQuery: How can I Make Two Text Inputs Effect Each Other?


Pixels <input type="text" name="mytext[]" id="Pixels" value="Text 1"></input>
    <br />
    Percentage <input type="text" name="mytext[]" id="Percentage" value="Text 1"></input>

Enter 10 in the first input, in the second input, how do I echo this *2 and for the other input /2 upon something being entered in the field.


Solution

  • var X = 100;
    
    var inp1 = document.getElementById('Pixels'),
        inp2 = document.getElementById('Percentage');
    
    inp1.onchange = function() {
        var num = this.value = Math.max(Math.min(this.value, X), 0);
        inp2.value = num / X * 100;
    };
    inp2.onchange = function() {
        var num = this.value = Math.max(Math.min(this.value, 100), 0);
        inp1.value = num * X / 100;
    };