vendredi 22 mars 2019

Javascript if condition with substring

I try to make some inputs to be completed according the "master" input. For instance, if I type 123 in master I want to have a specific text in every slave fields, but if I type 223 I want something else.

I managed to this and is working without the if conditions. With that I get this: slave.substring is not a function. Is important to use substring because I will have more fileds to be completed based on specific position of master value.

window.onload = function()
{
    var master = document.getElementById('value_master');
    var slave = document.getElementById('value_slave');

    document.getElementById('value_master').onkeyup = function(){
        if (slave.substring(0,1).equals("1") )          
        {
            slave.value = 'you typed something starting with 1';
        }
        else {
            slave.value = 'you typed something starting with 2';
        }
    };
};

Aucun commentaire:

Enregistrer un commentaire