Question

From the following a I need to get value from the data-autocomplete attribute (we have label and value in there but I need to collect only value) by using jQuery, please help.

<a href="nullAUS%7C6214051%7C0HOAUSAQjdBwAAAAAIAwEAAAAA2igAgAAAAAAAAABkAAAAAP....8AAAAAAAAAAABub3J0AA--" data-transition="fade" data-autocomplete="{&quot;label&quot;:&quot;North Chigwell Primary, 115 Allunga Road, CHIGWELL  TAS  7011&quot;,&quot;value&quot;:&quot;AUS|6214051|0HOAUSAQjdBwAAAAAIAwEAAAAA2igAgAAAAAAAAABkAAAAAP....8AAAAAAAAAAABub3J0AA--&quot;}" class="ui-link-inherit">North Chigwell Primary, 115 Allunga Road, CHIGWELL  TAS  7011</a>

This is code how I am trying :

callback: function (e) {

    var a = $(e.currentTarget); // access the selected item
    var address = $.trim(a.text());
    // data-autocomplete

    var moniker = $(a).attr("data-autocomplete");

    $('#addressQas').val(address);
    //$.ajax({
    new FancyAjaxinator().jsonPost({
        url: "/address/selectLine",
        //dataType: "json",
        data: {
            "moniker": moniker,
                "address": address
        },
        success: function (data) {
            //console.log(data.responseObject);
            var qasAddress = data.responseObject;
            $('#address-line1').val(qasAddress.addressLine1);
            if (qasAddress.state) {
                $('#address-line2').val(qasAddress.addressLine2);
                $('#address-townCity').val(qasAddress.suburb);
                $('#address-state').val(qasAddress.state);
                $('#address-state').parent().find('span.ui-selectmenu-text').text($("#address-state option[value=" + qasAddress.state + "]").text());
                $('#address-postcode').val(qasAddress.postcode);
                $('#address-line1').valid();
                $('#address-townCity').valid();
                $('#address-state').valid();
                $('#address-postcode').valid();
            }
            $('#QASaddressField').hide();
            $('.qasAddressFields').show();
            //setFocus();
        }
    });
    this.close;
    return false;
}
Was it helpful?

Solution

Try

var a =  $('a');// or the jQuery wrapper for the target a
var json =a.data('autocomplete');
console.log(json.value)

Demo: Fiddle

Using .attr()

var moniker =  $(a).attr("data-autocomplete");
var json = JSON.parse(moniker);
console.log(json.value);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top