K
K
Kirill Kaliev2020-01-15 20:20:05
Yii
Kirill Kaliev, 2020-01-15 20:20:05

Framework Yii2. Is it possible to set value in ActiveForm using div?

Dear programmers, I am writing to you with this question
. Is it possible to specify the value in the ActiveForm field->textInput form in Value?
Here's the thing: I connected JavaScript Yandex Api maps and a geocoder, wrote a simple documentary code so that the user can select a specific house or street on the map, and now I need to somehow extract this data from JS and place it in the form for sending
Here is the script code launching YandexMaps

<script type="text/javascript">
    ymaps.ready(init);

    function init() {
        var myPlacemark,
            myMap = new ymaps.Map('map', {
                center: [55.753994, 37.622093],
                zoom: 9
            }, {
                searchControlProvider: 'yandex#search'
            });

        myMap.events.add('click', function (e) {
            var coords = e.get('coords');

            if (myPlacemark) {
                myPlacemark.geometry.setCoordinates(coords);
            }
            else {
                myPlacemark = createPlacemark(coords);
                myMap.geoObjects.add(myPlacemark);
                myPlacemark.events.add('dragend', function () {
                    getAddress(myPlacemark.geometry.getCoordinates());
                });
            }
            getAddress(coords);
        });

        function createPlacemark(coords) {
            return new ymaps.Placemark(coords, {
                iconCaption: 'поиск...'
            }, {
                preset: 'islands#violetDotIconWithCaption',
                draggable: true
            });
        }

        function getAddress(coords) {
            myPlacemark.properties.set('iconCaption', 'поиск...');
            ymaps.geocode(coords).then(function (res) {
                var firstGeoObject = res.geoObjects.get(0);

                myPlacemark.properties
                    .set({
                        iconCaption: [
                            firstGeoObject.getLocalities().length ? firstGeoObject.getLocalities() : firstGeoObject.getAdministrativeAreas(),
                            firstGeoObject.getThoroughfare() || firstGeoObject.getPremise()
                        ].filter(Boolean).join(', '),
                        balloonContent: firstGeoObject.getAddressLine()
                    });
                var adress = firstGeoObject.getAddressLine();
                document.getElementById("adress").innerHTML = adress;
            });
        }
    }
</script>

Var adress is a variable with address chosen by the user.
using document.getElementById("adress").innerHTML = adress I pass the adress values ​​to the blob of the same name
That's why the question arose: is there any way to put it in value
If you know a way to pull a variable from JS and put it in ActiveForm please inform.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Musonov, 2020-01-16
@unclenoice

1. Assign a class to the desired input
2. Помещаете данные
Это с помощью jquery, если на чистом JS, то можно присвоить так же id и по нему работать

let address = document.getElementById('address');
address.value = 'my address';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question