Reputation: 5659
I'd like to get all the input tags with an id. Right now, my code is:
inputs = document.querySelectorAll('input');
for (i=0; i<inputs.length; i++) {
if (inputs[i].id) { .. }
}
I'd like something cleaner if possible. Thanks in advance.
Upvotes: 1
Views: 238
Reputation: 179266
Something like this?
var inputs = document.getElementsByTagName('input'),
i,
l = inputs.length;
for (i = 0; i < l; i++)
{
if (inputs[i].hasAttribute('id'))
{
//do some code
}
}
Edit to add:
Oops, turns out hasAttribute
isn't as cross-browser as I thought...
$('input[id]')
Upvotes: 0
Reputation: 238055
You can look for the existence of an attribute with [attribute name]
:
inputs = document.querySelectorAll('input[id]');
See the W3C docs on attribute presence selectors.
Upvotes: 6