How to take the scroll out of an input date field?

Asked

Viewed 136 times

1

I have an input date field, when I change the date and continue to focus on the field, if I use the scroll, the date changes. I need to disable this scroll when the field has selected.

  • 1

    Here I don’t have this behavior... What is the Browser and the Operating System you are having this problem?

  • Windows, Google Chrome

  • Not mine doesn’t do it... You are using trackpad or a conventional mouse with wheel?

  • link , in this example, if you select the year, for example, and use the mouse scroll, the year will change

  • if the focus is on the day, or month, it happens in them tbm

  • Testing locally it does not happen comingo in Win10 using Chrome, but there in the example of them on the site when I put in the field it does even this Scroll... you are testing it directly on their site or in your project you tb is with this behavior? Because there they may be using some script that alters the default behavior...

  • in my project I’m also with this behavior!! and I need to take this scroll of all inputs date pq I’m having problems with users

  • There is a script in W3schools that enables scrolling in the field. This scroll does not work in Chrome or any other browser. Maybe you’re using an outdated version of Chrome.

  • There’s no easy way to tell. You’d have to unlock their source code.

Show 4 more comments

2 answers

1

Good, you can disable the field input pattern input="date" with Javascript, for example.

let campoInput = window.document.querySelectorAll("input[type=date]");
for(let i = 0; i < campoInput.length; i++){
  campoInput[i].addEventListener("wheel", function(event)
  {
     event.preventDefault();
  })
}
label {display: block;margin-top:10px}
<label for="campo1">Campo 1</label>
<input type="date" name="" id="campo1">
<label for="campo2">Campo 2</label>
<input type="date" name="" id="campo2">
<label for="campo3">Campo 3</label>
<input type="date" name="" id="campo3">

In the code snippet above we have a variable campoInput that selects all the input guy date and places them in an array and in it is added an event with the addEventListener and the event is wheel that is triggered when the mouse wheel is rolled. And then it will call an anonymous function by disabling the field input pattern input="date" which is to scroll the mouse scroll and change the date.

A better way to do this script is with the loop for each, however, not supported in some browsers like IE. Follow:

let campoInput = window.document.querySelectorAll("input[type=date]");

campoInput.forEach(input =>{
    input.addEventListener("wheel", function(event);
})
  • 1

    Unnecessary to put an id in the field just for this. You can pick it up by type.

  • Yes, too, but the most common is you pick by id or class, but also works and gets option.

  • I believe that a thinner code is better, less bytes to be loaded. In addition, although the author cites "a field" in the title of the question, and in the comments she says "all fields", ie, with id can not do this in more than one field, unless she puts an id in each field, which makes it impossible to do this using id. But that’s okay. Your solution is not all bad, but tb is not ideal.

  • Well, I came by the question and not by the comments and in the author’s question it was apparently that the problem was in only one input and not in several input. So it was not my mistake, but the author’s mistake, because I answered the question clearly and solved the problem. And yes I also agree with you, as you say "I believe that a more streamlined code is better", but this code is appropriate yes to her question, but not to her comment :).

  • It would be better if I edited the question and put it in the plural and not in the singular.

  • The problem is that this "one" in the question can have a double interpretation: it can be a pronoun or a number. : / If it is a pronoun, it is undefined, which can be 1 or more fields.

  • I edited your reply, giving a way to use your script in all type fields date as she asked.

  • Okay, I’ll take it, thanks!

Show 3 more comments

0


You can try to solve with javascript

$('#seuForm').on('focus', 'input[type=date]', function (e) {
  $(this).on('mousewheel.disableScroll', function (e) {
  e.preventDefault()
  })
})
*{font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;}
input[type=date] {display: block;margin-bottom:10px;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="seuform" action="/action_page.php">
  Teste: <input type="date" name="teste">
  Teste 2: <input type="date" name="teste2">
  Teste 4: <input type="date" name="teste3">
  Teste 5: <input type="date" name="teste4">
  <input type="submit">
</form>

  • I could put an example with pure Javascript, because it is not known if the author of the question uses jQuery.

  • I suggest you explain what was done in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.