0

I have two inputs that are required in order for the form to be able to be submitted. The inputs are the latitude and longitude of an address. If i fill the fields manually, the required state of the field is updated and the form can be submitted.

However, if i fill it automatically by clicking on the map, the values are updated but the required state does not change. It stays red. To be more specific. If a user clicks on the map, or searches for an address, the marker will update its position and the fields will be updated with the new coordinates.

I am using Leaflet and GeoSearch so i use some of the events that are available to be able to achieve this. Here is my code so far:

let map = L.map('map').setView([lat, lon], 18);
var marker = new L.marker([lat, lon]).addTo(map)

map.on('geosearch/showlocation', function (e) {
    if (marker) {
        marker = updateFields(e.marker._latlng, marker, map);
    } else {
        marker = new L.Marker(e.marker._latlng).addTo(map);
    }
});

setTimeout(() => {
   map.invalidateSize(true);
});

map.on('click', function (e) {
    if (marker) {
        marker = updateFields(e.latlng, marker, map);
    } else {
        marker = new L.Marker(e.latlng).addTo(map);
    }
});
...
function updateFields(coordinates, marker, map) {
    /* get the coordinates inputs*/
    let hiddenLng = document.querySelector("input[name*='[marker_longitude]']");
    let userLng = document.querySelector("input[data-formengine-input-name*='[marker_longitude]']");
    let hiddenLat = document.querySelector("input[name*='[marker_latitude]']");
    let userLat = document.querySelector("input[data-formengine-input-name*='[marker_latitude]']");

    map.removeLayer(marker);
    marker = new L.Marker(coordinates).addTo(map);

    hiddenLng.value = coordinates.lng.toFixed(7);
    userLng.value = coordinates.lng.toFixed(7);
    hiddenLat.value = coordinates.lat.toFixed(7);
    userLat.value = coordinates.lat.toFixed(7);

    return marker;
}

This will update the visible part of the fields as well as the hidden ones. The CMS i am working on, requires hidden fields for the actual data to be submitted.

The question:

How do i update the required state of the filled inputs?

Here a visual representation with the structure of the inputs and the problem:

The structure

enter image description here

The unchanged required state when the fields are filled

enter image description here

Best regards

Aristeidis Karavas
  • 1,891
  • 10
  • 29
  • You know you can just use [`Document.querySelector()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) instead of using jquery if you only want the query functionality. In 2022 there isn't much point using jquery. It's pretty much all covered in core JS – Liam Jan 04 '22 at 11:17
  • Does this answer your question? [How to set HTML5 required attribute in Javascript?](https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript) – Liam Jan 04 '22 at 11:18
  • hey Liam. I am working 99% of the time as Backend Developer and i am not really informed on the new ways to use JS. Thanks for the tip, i will look into it. Do you think it is going to solve my problem? – Aristeidis Karavas Jan 04 '22 at 11:19
  • @Liam as far as i understand, the question you linked, removes the required attribute it doesnt update it right? – Aristeidis Karavas Jan 04 '22 at 11:20
  • Well it's a "property". So "updating" doesn't make any sense. It either exists or it doesn't. It does not hold any value – Liam Jan 04 '22 at 11:52
  • In jQuery world this is the [difference between `prop` and `attr`](https://stackoverflow.com/questions/5874652/prop-vs-attr) – Liam Jan 04 '22 at 11:53
  • then how does the red warning is going away when one manually writes something in the input? Something must be capturing this and lets HTML/JS know that this field is filled – Aristeidis Karavas Jan 04 '22 at 11:55
  • The browser does this. HTML or Js are not involved in this at all – Liam Jan 04 '22 at 12:05
  • Well that said, preumsing your using the standard HTML 5 `required` if your not, then who knows. Will depend on whatever validation library your using – Liam Jan 04 '22 at 12:07
  • alright. I might as well convert the fields to readOnly without them to be required. I will always give a standard value. Thanks! BTW i changed the code to DOM and removed all the jQuery :) – Aristeidis Karavas Jan 04 '22 at 12:23

0 Answers0