Martin Erlic
Martin Erlic

Reputation: 5667

Retrieve text from div and place into form input

   <div id="embeddedExample">
        <div id="embeddedCalendar">
        </div>
        <br/>
        <div id="embeddedDateField" class="dateField">
            Select a date from the calendar above.
        </div>
        <br/>
    </div>

I want to retrieve the current text from the div with id "embeddedDateField" and then put it into the following form:

<input id="dateInput" type="text" th:field="*{selectedDate}" style=""/>

I'm using this JS right now:

<script>
    // Access the input inside the div with this selector:
    $(function () {
      var myDate = $("#embeddedDateField").val();
      $("#dateInput").val(myDate);
      Console.log(myDate);
    });
</script>

The text is not appearing. What's wrong?

Upvotes: 0

Views: 32

Answers (1)

void
void

Reputation: 36703

Uset .text() to retrieve text content of a div and not .val(). .val() is used for input elements..

    $(function () {
      var myDate = $("#embeddedDateField").text();
      $("#dateInput").val(myDate);
    });

Upvotes: 3

Related Questions