Home > Software design >  Putting a variable into a form using plain Javascript
Putting a variable into a form using plain Javascript

Time:10-09

This is pretty plain and simple but I'm just scratching my head. I created a form (https://taa.be.wolterskluwer.com/l/940253/2021-10-08/5tgp6n) which contains a field GACLIENTID (I've assigned it the classes GA_Client_ID & GACLIENTID) however since the form is not handcoded the class is assigned to the contacting paragraph (which contains a label & input).

I've successfully pushed my variable using the following script:

<script>
  (function() {
    var list = document.querySelectorAll('input[name="940253_168501pi_940253_168501"]');
    list.forEach(function(el){
      if(el) {
        el.value = {{DL - GA Client ID}};
  }
})
  })();
</script>

But this finds the input with the "name" of the field which changes on every clone of the form I'm creating. So I'm looking for a more "universal" way of selecting the input within the class but I just can't, for the life of me, figure out the correct syntax.

Any help is much appreciated.

CodePudding user response:

var list = document.querySelectorAll('.GACLIENTID input');

CodePudding user response:

If there's only ever one matching field.

document.querySelector('.GACLIENTID input')?.value = {{DL - GA Client ID}};
  • Related