How do I dynamically update a <p> element when a date is selected from an input type="date"?

huangapple go评论79阅读模式
英文:

How do I dynamically update a <p> element when a date is selected from an input type="date"?

问题

I have a paragraph element that I'd like to dynamically update to display whatever the date selected from my input type="date" element is:

<input type="date" id="datepicker" name="date" required>

<p id='testVariable3'></p>

So far I've tried this, but it doesn't update:

<div>
  <h4 style="padding: 5px">Select a date</h4>
</div>

<input type="date" id="datepicker" name="date" required />

<script>
  var currentDate = new Date().toISOString().split("T")[0];
  document.getElementById("datepicker").min = currentDate;
</script>

<p id="testVariable1"></p>
<p id="testVariable2"></p>
<p id="testVariable3"></p>

<div>
  <input type="submit" value="Submit" class="btn btn-primary" />
</div>

<script>
  const hotdeskLinks = document.querySelectorAll(".test a");
  var allBookings = "{{all-bookings}}";
  var decodedBookings = allBookings.replace(/&quot;/g, '"');

  const test1 = document.getElementById("testVariable1");
  const workspaceAttributes = JSON.parse(decodedBookings).map(
    (decodedBookings) => decodedBookings.workspace
  );
  test1.textContent = workspaceAttributes;

  const test2 = document.getElementById("testVariable2");
  const dateAttributes = JSON.parse(decodedBookings).map(
    (decodedBookings) => decodedBookings.date
  );
  test2.textContent = dateAttributes;

  const datepicker = document.getElementById("datepicker");
  const test3 = document.getElementById("testVariable3");

  datepicker.addEventListener("change", function (event) {
    const selectedDate = datepicker.value;
    test3.textContent = selectedDate;
  });
</script>
英文:

I have a paragraph element that I'd like to dynamically update to display whatever the date selected from my input type="date" element is:

&lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required&gt;

&lt;p id=&#39;testVariable3&#39;&gt;&lt;/p&gt;

So far I've tried this, but it doesn't update:

&lt;div&gt;
  &lt;h4 style=&quot;padding: 5px&quot;&gt;Select a date&lt;/h4&gt;
&lt;/div&gt;

&lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required /&gt;

&lt;script&gt;
  var currentDate = new Date().toISOString().split(&quot;T&quot;)[0];
  document.getElementById(&quot;datepicker&quot;).min = currentDate;
&lt;/script&gt;

&lt;p id=&quot;testVariable1&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;testVariable2&quot;&gt;&lt;/p&gt;
&lt;p id=&quot;testVariable3&quot;&gt;&lt;/p&gt;

&lt;div&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;btn btn-primary&quot; /&gt;
&lt;/div&gt;

&lt;script&gt;
  const hotdeskLinks = document.querySelectorAll(&quot;.test a&quot;);
  var allBookings = &quot;{{all-bookings}}&quot;;
  var decodedBookings = allBookings.replace(/&amp;quot;/g, &#39;&quot;&#39;);

  const test1 = document.getElementById(&quot;testVariable1&quot;);
  const workspaceAttributes = JSON.parse(decodedBookings).map(
    (decodedBookings) =&gt; decodedBookings.workspace
  );
  test1.textContent = workspaceAttributes;

  const test2 = document.getElementById(&quot;testVariable2&quot;);
  const dateAttributes = JSON.parse(decodedBookings).map(
    (decodedBookings) =&gt; decodedBookings.date
  );
  test2.textContent = dateAttributes;

  const datepicker = document.getElementById(&quot;datepicker&quot;);
  const test3 = document.getElementById(&quot;testVariable3&quot;);

  datepicker.addEventListener(&quot;change&quot;, function (event) {
    const selectedDate1 = datepicker.value;
    test3.textContent = selectedDate;
  });
&lt;/script&gt;

答案1

得分: 2

你的脚本底部似乎有一个小拼写错误。这是你的代码:

datepicker.addEventListener('change', function(event) {
const selectedDate1 = datepicker.value;
test3.textContent = selectedDate;

你在事件监听器函数中声明了 selectedDate1,然后尝试将 selectedDate 赋值给 test3.textContent

看看修改这个拼写错误是否可以解决你的问题。

英文:

It looks like you have a small typo at the bottom of your script. This is your code:

datepicker.addEventListener(&#39;change&#39;,  function(event) {
const selectedDate1 = datepicker.value;
test3.textContent = selectedDate;

You declared selectedDate1 in your event listener function and then tried to assign selectedDate to test3.textContent

See if changing this typo can fix your issue.

答案2

得分: 0

你的代码可以工作!只需确保在HTML代码中,在创建inputp元素之后加载脚本。

像这样:

&lt;body&gt;
    &lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required&gt;
    &lt;p id=&#39;dynamicDate&#39;&gt;&lt;/p&gt;

    &lt;script&gt;
        const datepicker = document.getElementById(&#39;datepicker&#39;);
        const test = document.getElementById(&#39;dynamicDate&#39;);

        datepicker.addEventListener(&#39;change&#39;, function(event) {
        const selectedDate = datepicker.value;
        test.textContent = selectedDate;
        });
    &lt;/script&gt;
&lt;/body&gt;

在你的代码中,你可以这样做:

&lt;div&gt;
    &lt;h4 style=&quot;padding: 5px; &quot;&gt;选择日期&lt;/h4&gt;
&lt;/div&gt;

&lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required&gt;

&lt;script&gt;
    var currentDate = new Date().toISOString().split(&quot;T&quot;)[0];
    document.getElementById(&quot;datepicker&quot;).min = currentDate;
&lt;/script&gt;

&lt;p id=&#39;testVariable1&#39;&gt;&lt;/p&gt;
&lt;p id=&#39;testVariable2&#39;&gt;&lt;/p&gt;
&lt;p id=&#39;testVariable3&#39;&gt;&lt;/p&gt;

&lt;div&gt;
    &lt;input type=&quot;submit&quot; value=&quot;提交&quot; class=&quot;btn btn-primary&quot;&gt;
&lt;/div&gt;

&lt;script&gt;
    // 将这段代码从第二个脚本标签中分离出来
    const datepicker = document.getElementById(&#39;datepicker&#39;);
    const test3 = document.getElementById(&#39;testVariable3&#39;);

    datepicker.addEventListener(&#39;change&#39;, function (event) {
        const selectedDate = datepicker.value;
        test3.textContent = selectedDate;
    });
&lt;/script&gt;

&lt;script&gt;
    const hotdeskLinks = document.querySelectorAll(&#39;.test a&#39;);
    var allBookings = &#39;{{all-bookings}}&#39;;
    var decodedBookings = allBookings.replace(/&amp;quot;/g, &#39;&quot;&#39;);

    const test1 = document.getElementById(&#39;testVariable1&#39;);
    const workspaceAttributes = JSON.parse(decodedBookings).map((decodedBookings) =&gt; decodedBookings.workspace);
    test1.textContent = workspaceAttributes;
    const test2 = document.getElementById(&#39;testVariable2&#39;);
    const dateAttributes = JSON.parse(decodedBookings).map((decodedBookings) =&gt;
        decodedBookings.date);
    test2.textContent = dateAttributes;

    // 原本在这里
&lt;/script&gt;

这里我只是将第二个脚本标签分成两部分,以便单独运行!(还有一个小错别字!)

希望这有所帮助 How do I dynamically update a <p> element when a date is selected from an input type="date"?

英文:

Your code works! You just have to make sure that in the HTML code, you're loading the script after you create the input and p elements in the body.

Something like this:

&lt;body&gt;
    &lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required&gt;
    &lt;p id=&#39;dynamicDate&#39;&gt;&lt;/p&gt;

    &lt;script&gt;
        const datepicker = document.getElementById(&#39;datepicker&#39;);
        const test = document.getElementById(&#39;dynamicDate&#39;);

        datepicker.addEventListener(&#39;change&#39;, function(event) {
        const selectedDate = datepicker.value;
        test.textContent = selectedDate;
        });
    &lt;/script&gt;
&lt;/body&gt;

To do this in your code, you can do it like this:

&lt;div&gt;
    &lt;h4 style=&quot;padding: 5px; &quot;&gt;Select a date&lt;/h4&gt;
&lt;/div&gt;

&lt;input type=&quot;date&quot; id=&quot;datepicker&quot; name=&quot;date&quot; required&gt;

&lt;script&gt;
    var currentDate = new Date().toISOString().split(&quot;T&quot;)[0];
    document.getElementById(&quot;datepicker&quot;).min = currentDate;
&lt;/script&gt;

&lt;p id=&#39;testVariable1&#39;&gt;&lt;/p&gt;
&lt;p id=&#39;testVariable2&#39;&gt;&lt;/p&gt;
&lt;p id=&#39;testVariable3&#39;&gt;&lt;/p&gt;

&lt;div&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot; class=&quot;btn btn-primary&quot;&gt;
&lt;/div&gt;

&lt;script&gt;
    // Moved this code out of the second script tag
    const datepicker = document.getElementById(&#39;datepicker&#39;);
    const test3 = document.getElementById(&#39;testVariable3&#39;);

    datepicker.addEventListener(&#39;change&#39;, function (event) {
        const selectedDate = datepicker.value;
        test3.textContent = selectedDate;
    });
&lt;/script&gt;

&lt;script&gt;
    const hotdeskLinks = document.querySelectorAll(&#39;.test a&#39;);
    var allBookings = &#39;{{all-bookings}}&#39;;
    var decodedBookings = allBookings.replace(/&amp;quot;/g, &#39;&quot;&#39;);

    const test1 = document.getElementById(&#39;testVariable1&#39;);
    const workspaceAttributes = JSON.parse(decodedBookings).map((decodedBookings) =&gt; decodedBookings.workspace);
    test1.textContent = workspaceAttributes;
    const test2 = document.getElementById(&#39;testVariable2&#39;);
    const dateAttributes = JSON.parse(decodedBookings).map((decodedBookings) =&gt;
        decodedBookings.date);
    test2.textContent = dateAttributes;

    // Originally here
&lt;/script&gt;

All I've done here is separated the second script tag into 2 so that it runs separately! (Also a small typo!)

Hope this helped How do I dynamically update a <p> element when a date is selected from an input type="date"?

答案3

得分: 0

没有问题的代码。但是,重要的是要知道“change”监听器只会在填写完日期的所有组件(例如月份、年份和日期)之后才会触发。

How do I dynamically update a <p> element when a date is selected from an input type="date"?

英文:

There's nothing wrong with your code. However, it's important to know that the "change" listener will only be triggered after filling out all of the components of the date (e.g. month, year, and day).

How do I dynamically update a <p> element when a date is selected from an input type="date"?

huangapple
  • 本文由 发表于 2023年6月29日 23:23:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定