Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:52:36+00:00 2026-05-30T16:52:36+00:00

I have written the following jquery code block. When a postcode is changed on

  • 0

I have written the following jquery code block. When a postcode is changed on my site this code is run but it only works fully the first time the postcode is changed. I have put three alerts in the code. When the code block has been run once I don’t get as far as the third alert. Am i doing something wrong?

$('#postcodefield').change(function(){
    var newPC = $(this).val()
    var areaData = null;
    var MAPITURL = "http://mapit.mysociety.org/postcode/";
    var postcodeUrl = MAPITURL + newPC;

    $('#gpQueryTerm').val(newPC);

    $.ajax({
        type : 'GET',
        async: false,
        url : postcodeUrl,
        dataType : 'json',
        error: function(){
            $('input[name=town]').val(6);
        },
        success: function(data){
            alert('1');
            areaData = data;
            alert('2');
            // Extract the ward number
            ward = areaData.shortcuts.ward;
            wardCode = areaData.areas[ward].codes.ons;
            // now we have the ons, get the townsend value from the db
            alert('3');
            $.post("/tasks/getTownsend.cfm", {
                code: wardCode
            }, function(data){
                $('input[name=town]').val(data);
            });
        }
    });
});

Thanks in advance

James

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:52:38+00:00Added an answer on May 30, 2026 at 4:52 pm

    If the third alert is never being called then there must be an error with:

    ward = areaData.shortcuts.ward;
    wardCode = areaData.areas[ward].codes.ons;
    

    Given the data in the question that’s the most detailed answer I can give. I suggest you take a look at the JSON response and make sure it’s what you expect it to be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following code. But it is removing only &nbsp; not <br>
I have the following code, written using jQuery: var strval = '22' $(#quicknote).attr(href, javascript:location.href=/'http://site.com/notes/add?projects=/'+
I have written the following function but it's isn't returning anything when I run
This following code snippet works fine using jQuery1.2.3, but it doesn’t work with latest
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another
I have written the following: var pages=[[www.google.co.uk] This is the WWW. ,[www.yahoo.co.uk] This is
i have written the following code: As you can see there is a for
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I have written following code to set the status of the image button to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.