1

I am trying to fetch details of the id which element is changed. But when I am trying to find it using below code, it is giving error.

HTML:

<input type="text" required="required" placeholder="Enter city name" class="get_neighborhood" maxlength="30" id="city" value="P" name="city" data-invalid="">
<input type="text" placeholder="State code" maxlength="2" class="get_neighborhood" required="required" id="state" value="AA" name="state">

JS:

$(document).ready(function() {
    $(".get_neighborhood").on('change',function() {
        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = $(this).attr('id');                
                //var id=this.id;
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });
    });
});

this gives error that myid is undefined. when I change city or state field.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
TechCare99
  • 1,763
  • 6
  • 24
  • 33

3 Answers3

1

Try to cache the $(this) reference outside that ajax call and use it inside of that.

Full code:

$(".get_neighborhood").on('change',function() {

        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';
        _this = $(this);

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = _this.attr('id');                
                //var id=this.id;
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });

});
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
1

In ajax, you lose the element event context so you cannot access it inside ajax, what you need to do is store it's id after event in variable and the use it in scucess:

$(".get_neighborhood").on('change',function() {
        city = $('#city').val();
     var Id = this.id;

     //or

    var element = $(this);

......

.....

success: function(output_value) {
                myid = Id;            

}

or store element reference and use like this :

$(".get_neighborhood").on('change',function() {

        var element = $(this);

    ......

    .....

    success: function(output_value) {
                    myid = $(element).attr("id");            

    }
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

You are using $(this) in the wrong context. Store event source in some variable and use it in success.

$(document).ready(function() {
    $(".get_neighborhood").on('change',function() {
        source = $(this); //Store $(this);
        city = $('#city').val();
        ajax_url = 'alternate_address/ajax_get_country/';

        $.ajax({
            type: 'POST', // or 'POST', whatever you want.
            dataType: 'json', // output_value will be a plain text string.
            data: {
                city: city
            },
            url: ajax_url,
            beforeSend: function(msg) {                

            },
            success: function(output_value) {
                myid = source.attr('id'); //Use source here.                
                //var id=this.id; 
                console.log(myid.toSource());

            },
            error: function(output_value) {
                alert('err');                    
            }
        });
    });
});
Adil
  • 146,340
  • 25
  • 209
  • 204