Search code examples
javascriptinputelement

How to get the id value from a function from outside the function


I have this code that dynamically produces the id of an element.

function get_data_mapping_for_id(path) {

    let id = $(path).attr('data-map-id');

    Swal.fire({
        title: 'Loading data...',
        willOpen: function() {

            $('.site-plan').addClass("zoom-svg");
            $('path[data-map-id="' + id + '"]').addClass("highlight-path")

            Swal.showLoading();
            $.get(endPoint, {
                action: 'get_data_mapping_for_id',
                id
            }, function(data) {
                Swal.hideLoading();
                jsonResp = JSON.parse(data);
                const table = jsonToHTMLTable(jsonResp, $(path));
                Swal.update({
                    title: jsonResp.title,
                    html: table,
                    confirmButtonText: 'OK'
                });
            });
        },
        willClose: function() {
            $('.site-plan').removeClass("zoom-svg");
            $('path[data-map-id="' + id + '"]').removeClass("highlight-path");
        }
    });
}

I need to set the value of a dynamcally created input with the 'id'.

BUT I cannot access the 'id' outside of the code.

AND I cannot set the input value while inside the code.

what line of code do I use to get the 'id' outside the code so I can use it?

I tried setting the input value without using the 'id' outside the code like this: $('#pIP').val("8"); and it works.

I tried setting the input value without using the 'id' inside the code like this: $('#pIP').val("8"); and it does not work.

I tried setting the input value using the 'id' outside the code like this: $('#pIP').val(id); and it does not work.

I tried setting the input value using the 'id' inside the code like this: $('#pIP').val(id); and it does not work.


Solution

  • You can "return" the id value or set it as a global variable.