html - get index of input element in javascript -
if have 3 input boxes on web page , user clicks second input, need input index, position of input on page. need in pure javascript. have far doesn't work...
document.queryselector('html').onclick = function (e) { log(e); } function log(obj) { var nodename = obj.target.nodename var idx = nodename.length console.log(nodename, idx); }
any appreciated!
pure javascript:
function getindexfromset(set, elm){ var setarr = [].slice.call(set); for( var in setarr ) if( setarr[i] == elm ) return i; }
the above function can used so:
function checkinputfocus(e){ if(e.target && e.target.nodename == 'input' ) index = getindexfromset(inputs, e.target); } var inputs = document.queryselectorall('input'); document.addeventlistener("click", checkinputfocus);
using jquery, if run on page (in console)
var inputs = $('input'); // input elements on page inputs.index( $('#save-pinned-sites-btn') ); // find index of spesific 1
you number representing index of $('#save-pinned-sites-btn')
element
Comments
Post a Comment