0

Hi there can you help me solving this, I have this code here

<div>
   <div>
      <input type="text" name="" tabindex="0" styling="[object Object]" 
      placeholder="YYYY/MM/DD" aria-label="date" aria-invalid="false" 
      valid="1" class="needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 
      fsIKy kl-private-reset-css-Xuajs1">
  </div>
</div>
<div>
   <div>
      <input type="text" name="" tabindex="0" styling="[object Object]" 
      placeholder="YYYY/MM/DD" aria-label="date" aria-invalid="false" 
      valid="1" class="needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 
      fsIKy kl-private-reset-css-Xuajs1">
  </div>
</div>

How can i give the input value today's date, with pure javascript, i cant add manually and can't add other classes name or id's because its automatically generated. Thanks,

Edited, The script i want to include is in a shopify theme, and the inputs are on a pop-up, maybe this will help you more

Endrit Shabani
  • 190
  • 1
  • 12
  • Does this answer your question? [How to set input type date's default value to today?](https://stackoverflow.com/questions/6982692/how-to-set-input-type-dates-default-value-to-today) – Jelle Dec 04 '20 at 07:37
  • If the input element is the first input element on the page with the class of "test", you can get a reference to it with simple `document.querySelector('input.test')`. – Teemu Dec 04 '20 at 07:42

5 Answers5

1

This should help:

let date = new Date();
document.getElementsByClassName('needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 fsIKy kl-private-reset-css-Xuajs1')[0].value = date.getFullYear() +'/'+ date.getMonth() +'/'+ date.getDate();
document.getElementsByClassName('needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 fsIKy kl-private-reset-css-Xuajs1')[1].value = date.getFullYear() +'/'+ date.getMonth() +'/'+ date.getDate();
<div>
   <div>
      <input type="text" name="" tabindex="0" styling="[object Object]" 
      placeholder="YYYY/MM/DD" aria-label="date" aria-invalid="false" 
      valid="1" class="needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 
      fsIKy kl-private-reset-css-Xuajs1">
  </div>
</div>
<div>
   <div>
      <input type="text" name="" tabindex="0" styling="[object Object]" 
      placeholder="YYYY/MM/DD" aria-label="date" aria-invalid="false" 
      valid="1" class="needsclick TextInput__FormStyledTextInput-sc-1o6de9f-0 
      fsIKy kl-private-reset-css-Xuajs1">
  </div>
</div>
Jelle
  • 758
  • 2
  • 14
  • 36
1

You can use getElementsByClassName() method to get the elements with class "test" and then use "Date()" to pass the date.

inputs = document.getElementsByClassName("test")
inputs[0].value = new Date();
<div>
  <div>
    <input class="test" type="text" />
  </div>
</div>
<div>
  <div>
    <input class="test" type="text" />
  </div>
</div>
Harshana
  • 5,151
  • 1
  • 17
  • 27
1

Here you go :

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

for (let i = 0; i<inputs.length; i++) {
    if (inputs[i].classList.contains('test')) {
        let now = new Date();
        inputs[i].value = now.getFullYear() +'/'+ now.getMonth() +'/'+ now.getDate();
        break;
    }
}
<div>
   <div>
      <input class="test" type="text"/>
  </div>
</div>
<div>
   <div>
      <input class="test" type="text"/>
  </div>
</div>
Obzi
  • 2,384
  • 1
  • 9
  • 22
0
const inputs = documemt.querySelectorAll('.test')
for (let i = 0; i < inputs.length; i++) {
    inputs[i].value = new Date().getDate()
} 

EDIT

If you only want a single input to display the date:

inputs[index]  = new Date().getDate()
kmp
  • 692
  • 6
  • 17
0

var now = new Date();

document.getElementsByClassName("test")[0].value = now;

document.getElementsByClassName("test")[1].value = now;

Ziv Adler
  • 169
  • 2
  • 11