Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

How to get all inputs [JS]

How would I get all inputs, then find the one that matches the value of "07" and grab that full value to be able to pass to Ajax with vanilla Javascript?

I have tried the following but to no avail.

var matches = [];
var i = '';

   for(i in document.getElementsByTagName('input')) {
      if(i.value === '07') {
         matches.push(i);
      }
     console.log(matches);
   }
0 likes
33 replies
zachleigh's avatar

Maybe this?

var matches = [];

var inputs = document.getElementsByTagName('input');

for(var key in inputs) {
    var value = inputs[key].value;

    if(value === '07') {
        matches.push(value);
    }
}

console.log(matches);
theUnforgiven's avatar

@ohffs As you can see form the JSBin I have the same but doesn't seem to work. Just shows empty []

theUnforgiven's avatar

Ah yes, see what you've done there, by given each input a value, sorry I forgot to mention this needs to happen when focus is moved from one input.

ohffs's avatar

Ahhh :-) My raw JS skills are already stretched doing that ;-) I'd be onto jquery or vue to do anything like that - it'd be fairly easy with vue I think. Most things are before I try them mind you... ;-)

ohffs's avatar

My old-school JS would shove an onblur onto each input that called the code as a function. Which is probably a bad way to do it. Something like :

html:
  Enter your name: <input type="text" id="fname" value="07" onblur="getMatches()"><br><br>

js:
function getMatches() {
  var inputs = document.getElementsByTagName('input');
  var matches = [];

  for(var key in inputs) {
    var value = inputs[key].value;

    if(value === '07') {
        matches.push(value);
    }
  }
  console.log(matches);
}
1 like
theUnforgiven's avatar

I won't have access to the inputs as this is part of an API I'm building, so just need to grab every input, get the one that starts with "07" then on focus out or something pass that to Ajax.

zachleigh's avatar

Similar to ohffs answer:
html

Enter your name: <input type="text" id="fname" value="07"  onfocusout="getInputs()"><br><br>
  
Enter your lname: <input type="text" id="lname" value="07"  onfocusout="getInputs()">

Javascript

function getInputs() {
  var matches = [];

  var inputs = document.querySelectorAll('input');

  for(var i = 0; i < inputs.length; i++) {
      var value = inputs[i].value;

      if(value === '07') {
        matches.push(value);
      }
  }

  console.log(matches);
}

Sometimes I find the javascript for-in syntax difficult so I go for the good old for loop...

1 like
theUnforgiven's avatar

I won't have access to the inputs as this is part of an API, so adding stuff like onfocusout="getInputs()" isn't possible. Just need to be able to get all inputs on focus out then find the one that as 07 in it to then use that and post with Ajax.

ohffs's avatar

Sooo you mean you can't change the html, but only the javascript? Or something else...?

theUnforgiven's avatar

Yes I won't be able to change the HTML only have access the JS.

ohffs's avatar

This seems to work :


function getMatches(event)
{
  var matches = [];

  var inputs = document.getElementsByTagName('input');

  for(var key in inputs) {
    var value = inputs[key].value;

    if(value === '07') {
        matches.push(value);
    }
  }

  console.log(matches);
}

inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
  inputs[i].addEventListener('blur', getMatches, true);
}

Not sure if blur is the right event, but you get the idea :-)

Edit: I seem to remember some weirdness with IE not having addEventListener but some other function instead - no idea if that's still the case. Works in FF anyway ;-)

1 like
theUnforgiven's avatar

Yeah although not working on JSBin, just showing empty arrays again, but will try within my code.

theUnforgiven's avatar

Are you have the value set though so yeah it will work, whereas when this happens on customers sites, it will only fire on focus out and grab the input starting with "07"

ohffs's avatar

Is that not what you asked for? I'm lost again....

theUnforgiven's avatar

Lol, yeah took me a while to understand it.

Maybe this will help:

I have add the following script built - https://gist.github.com/anonymous/36224b0540ab600480be62a900cca4f8

Which basically work in the way like Google Analytics works, by placing a piece of code like shown below to a customers website.

https://gist.github.com/anonymous/2e41f65b147a8c42a13a0ea9caffeb96

Which then I can use the endpoints to grab the data. So the script itself is fine I just need the following adding, so it works and functions across many sites all on different servers and passes the correct data.

So I need the following adding:

Ability to track whether a user leaves the screen when the above script is on a website. Whether a user moves to another tab Collect any input that starts with "07"

Ability to pass these to the script and to the end points so we can collect this via PHP and then do whatever we need to do with it.

I did this in VueJS - gist.github.com/anonymous/f2f7568d59d8c6ac8bb6c52d9df1c631 so this works the way I want it, but I need to translate this to Vanilla JS and where to put within the original script (first link posted)
ohffs's avatar

Ah - that's well beyond my JS knowledge I'm afraid! I'm usually just pleased I can make an element fade in and out with jquery ;-)

1 like
theUnforgiven's avatar

Still looking for advise on this please.

Based on the below provided by @ohffs still need to get all inputs, find the one that starts with 07 and on focusout.

function getMatches(event)
{
  var matches = [];

  var inputs = document.getElementsByTagName('input');

  for(var key in inputs) {
    var value = inputs[key].value;

    if(value === '07') {
        matches.push(value);
    }
  }

  console.log(matches);
}

var inputs = document.getElementsByTagName('input');

for(var i = 0; i < inputs.length; i++) {
  inputs[i].addEventListener('focusout', getMatches, true);
}

But this code doesn't seem to be working as expected, so any help greatly appreciated.

d3xt3r's avatar

@lstables If you are working on client site, they might have onBlur, onFocus assigned to their input fields, you should not be overriding them as it may lead to unusual behaviour on their part.

theUnforgiven's avatar

@d3xt3r How else can I get any given input that has 0700000000 within it then, using raw JS? I just need to get a mobile/cell phone number

theUnforgiven's avatar

@anonymous Yeah I see how that's working, but I need the actual value (the number the user would input) that starts/matches 07 as it will be a UK mobile number of 11 digits.

Next

Please or to participate in this conversation.