0

I am trying to submit HTML forms whenever a text input is entered so I have-

    <script type="text/javascript">

    $("#HA15CRNK13").blur(function() { 
    $("#HA15CRNK13A").submit();
    });

    $("#HA15VSPS13").blur(function() {
    $("#HA15VSPS13A").submit();
    });   

    </script>

The above works. However, I will like to achieve the same result in a for loop i.e

    <script type="text/javascript">
    var T = ['#HA15CRNK13','#HA15VSPS13'];
    var arrayLength = T.length;

    for (var i = 0; i < arrayLength; i++)
    {   
        var J = '"'+ T[i]+'"';      // id of input text element
        var P = '"'+ T[i]+'A'+'"'; // id of the form to submit
        $(J).blur(function() {
        $(P).submit();
        });
    }
    </script>

The above doesn't work. How do I go about this? Thanks.

2 Answers2

0

You don’t need as the variable type is string.

var J = T[i];    // id of input text element
var P = J+'A'; // id of the form t
DatGeoudon
  • 342
  • 3
  • 15
0

Why not like so:

var inputs = ['HA15CRNK13','HA15VSPS13'];

inputs.forEach(function (input) {
  var id = '#' + input;
  var formId = id + 'A';

  $(id).blur(function () {
    $(formId).submit();
  })
});
sliptype
  • 2,804
  • 1
  • 15
  • 26