I am a newbie JQuery/JavaScript user having a problem and cannot understand why my div data attribute is empty after saving/retrieving the DOM data. I need a little help if possible with examples to explaining what I am doing wrong. I have spent a few hours on this with no light in sight.
    // HTML
    <div id="outer">
        <div id="mydiv" data-myval=""></div>
    </div>
    // JS file body
    function getSomeData(){
        var url = "http://ip-api.com/json";
        $.ajax({
            url: url,
            dataType: "json",
            cache: false,
        }).done(function(data) {
            //var myjson = data;
            var myjson = JSON.stringify(data);
            $("#mydiv").attr("myval", data);  // setter
        }, "json");
    }
    $(document).ready(function()
    { 
        getSomeData();
        // get json data from attr data-myval
        //var object = JSON.parse(myjson);
        var myjson = $("#mydiv").data();  //getter
        alert(myjson);  
        if(typeof object === 'undefined') 
        {
          alert('No data found!');
        }
        else
        {
            // do something
            var lat = object.lat;
            var lng = object.lon;
            // more codes
        }
    }
 
     
    
getSomeDatain a different file? – Carl Barrett Feb 04 '19 at 18:25