HTML5 jQuery select all date fields 'input:date'
18,299
Solution 1
If you want something cross browser, you should stick with:
$('input[type="date"]')
I don't think you can select all inputs of type date in a different way. If however you want to select all text fields you can go with
$(':text')
or if you want all radios you can go with
$(':radio')
Solution 2
The cleanest way is to add a class "date" to all your input and select them with $('.yourClass')
.
If you can't add class, i don't see why $("input[type='date']")
"look no good".
Maybe you want something like $('input').filter('[type=date]')
?
Author by
Roar
Updated on June 14, 2022Comments
-
Roar almost 2 years
Is there a way to select all date inputs?
I have:<input type="date" name="bday">
all i need it's just to select all inputs.
there are many selectors, but nothing like:$('input:date')
this approach
$("input[type='date']")
looks no good
What's the best practice?
-
Robert McKee almost 11 yearsBecause adding a class isn't the cleanest way?
-
Karl-André Gagnon almost 11 yearsHow isnt
$('.date')
not cleaner than$("input[type='date']")
. Yes it add something to the DOM, but it is using class make the code shorter and it run faster aswell. And I also clearly said that$("input[type='date']")
is the second best way. Please, explain me if I am missing something, cause I can't see it. -
Robert McKee almost 11 yearsBecause adding redundant classes to markup is just adding garbage. It doesn't add anything, other than polluting the markup, hence, it is definitely not the cleanest way. The rest of your answer was correct. It also deviates from what the original question was. Selecting a class does not select all input of type date. What if another user later adds another input of type date? Selecting by class would then fail.
-
Robert McKee almost 11 yearsAdditionally, "run faster" should be "may run faster", I think you will find that IE 8, and opera will actually run faster by selecting a tag + attribute than by class. So it varies by browser and browser version, and it is usually pretty close performance wise.