Using jQuery to pull text from a specific <td> - javascript
I'm running an AJAX query on an external page, and am attempting to only return the data from the County . My current script is pulling the text from all of the table cells, but I cannot for the life of me get it to simply pull the county name.
The current script that is being run:
$( ".zipCode" ).each(function( intIndex ){
var zipCodeID = $(this).attr('id');
console.log('http://www.uscounties.org/cffiles_web/counties/zip_res.cfm?zip='+zipCodeID);
$.ajax({
url: 'http://www.uscounties.org/cffiles_web/counties/zip_res.cfm?zip='+zipCodeID,
type: 'GET',
success: function(res) {
var headline = $(res.responseText).find("p").text();
console.log(headline);
$('#'+zipCodeID).empty();
$('#'+zipCodeID).append(headline);
}
});
});
An example of the page that is being queried:
http://www.uscounties.org/cffiles_web/counties/zip_res.cfm?zip=56159
This should work for all entered ZIPS. The page layout is the same, I just can't get the function to return only the county. Any help or advice would be awesome. Thanks!
With the complete lack of ids and classes on that page, you don't really have much to go on. If you have access to the source of that page, stick an id or class on the cell and make your life so much easier. If not, you'll have to use what you know about the structure of the pages to find the county. Something like this will work specifically on that one page you linked to. If other pages have slight variations this will fail:
var headline = $(res.responseText).find("table > tr:eq(2) > td:eq(3)").text();
This assumes that there is only ever one table on the page and that the county is always in the 3rd cell of the 2nd row.
You're basically screen scraping. I somehow think you'll have issues with this due to cross domain and other things, but that is ancillary to the question.
You need to walk through the resultant page. Assuming there is only ever one page on the screen, it'll look like something this:
var retVal = [];
// Basically, for each row in the table...
$('tr').each(function(){
var pTR = $(this);
// Skip the header row.
if (pTR.find('th').length == 0)
{
// This is the array of TDs in the given row.
var pCells = $('td', pTR);
retVal.push({state:$(pCells[0]).text(), place:$(pCells[1]).text(), county:$(pCells[2]).text()});
}
});
// retVal now contains an array of objects, including county.
if (retVal.length > 0)
{
alert(retVal[0].county);
}
else
{
alert('Cannot parse output page');
}
The parsing code is written to be extensible, hence you get back all of the data. With postal codes, although you will likely only ever get back one county, you'll definitely get back more places. Also note... not every zip code has a county attached for a variety of reasons, but you should get back an empty string in that case.
Related
How to get the index of one specific element within an javascript object
I have been working on this problem for the second day now and I simply can't seem to find the right way to do it. Problem: I need to send the index value of a chosen element within a javascript object to an action in PHP. So when the user chooses a certain element, the index value of said element within the object should be send to the mentioned PHP action. What I have so far is the following: The button for the jump: <!-- Button to jumpo tp specific dashboard. The select and and option tags will be generated dynamicly --> <div style="float:right; margin-right: 10px;"> <select name="idDashboards" id="idDashboards"> <option value="">Jump to Dashboard</option> </select> </div> The Array itself as well as how the HTML elements are created // The array with the dashboards as I get it from the server var dashboardArray = <?php echo json_encode(CHtml::listData($dashboards, 'iddashboard', 'title')); ?>; // Loop through the elements in the dashboardArray and then create option elements to be added to the above create select element. for (var idDashboard in dashboardArray) { $('#idDashboards').append($(document.createElement('option')).prop({ value: idDashboard, text: dashboardArray[idDashboard].charAt(0).toUpperCase() + dashboardArray[idDashboard].slice(1) })); }; How the object looks like when I log it. {2: "Dashboard_Test_1", 3: "Dashboard_Test_2", 4: "Dashboard_Test_3", 6: "Dashboard_Test_4"} What I have been trying so far: var dashboardIndex = -1; var i; // Loop to get the indexes. for (i in dashboardArray) { if (dashboardArray.hasOwnProperty(i)) { dashboardIndex++ console.log(dashboardIndex+" These are the indexes"); } } // Ajax request that sends data to the PHP action $("#idDashboards").change(function() { var idDashboard = $(this).val(); console.log("The change happened!"); $.ajax({ url: 'Path/To/PHP/File/And/Action=' + dashboardIndex, method: 'GET', success: function(result) { location.reload(); console.log("So far, so good!"); } }); }); }); Now, the dashboardIndex and the index of the elements in the object align and are good. But how do I send the index value of only the element that has been chosen by the user? I am very sorry if this is a duplicate but I could not find anything helpful so far. Every advice/help is appreciated.
Inside your change function, you should be able to use $(this).prop('selectedIndex') to get the index of the item you selected. I am curious, however, why the API is looking for the index of the dashboard rather than the seemingly unique ID that your API is initially responding with. This may be a more resilient approach.
Django: populate the field based on previous field value - missing the last step to make it work
Like many, I want to populate a field in a django form based on what is selected in another field. I've read alot of answers with javascript(I struggle in javscript, so that's where I'm having trouble with the exemples), and I almost got it working, but the last step(updating the field itself) isn't working so I'd love some help with that part. Here are the 2 fields. The first fieldthat gets populated from a query and is located in a div named #merch in the form merchandise = forms.ModelChoiceField(label='Merchandise', queryset=Merchandise.objects.all(), merch_price = forms.DecimalField(label='Price', min_value=0, max_value=800, initial='0.00',decimal_places = 2, max_digits=10) Upon selection, the second field(div named #price) should then display the price based on the merchandise selected. I created the view for the ajax request: def check_item_price(request): if request.method == "GET": item = request.GET.get('item', '0')#the zero as default doesn't seem to work. To verify price = Merchandise.objects.get(id = item) return JsonResponse(price.item_price, safe=False)#is it safe to turn safe off? and the url url(r'^_item_price', views.check_item_price, name='_item_price' ) Calling the url manually works great, it returns the price in json format And here is the javascript that is in the html form. The first part works, upon change it calls the url and a json object is returned, but the second part that should update the second field isn't working. I admit my lack of knowledge in javascript is probably at fault here. I tried many variations based on examples, none worked for me. <script type="text/javascript"> jQuery(document).ready(function() { $('#merch').change(function() { var item = $(this).find(':selected').val(); $.getJSON('/classes/_item_price/',{item:item}, function(data) { $('#price').append("<option value=" + data.value + "></option>"); }); }); }); </script> Any pointers on what to fix in the javascript? Thanks!
After letting it marinate in my head for 2 months, I went back to it and finally made it work. Here is the right code jQuery(document).ready(function() { $('#merch').change(function() { var item = $(this).find(':selected').val(); $.getJSON('/classes/_item_price/',{item:item}, function(data) { document.getElementById('id_merch_price').value=data; }); }); }); </script> First, the ID wasn't precise enough, but also the way of updating it wasn't the right one it seems. I truly feel lost anytime I have to do research on javascript or jquery. So may ways to do the same thing, it's almost impossible to learn for a casual coder like me.
Shopify Access a product with its id on thank you page without using '/admin' in url
I am trying to access a specific product using its id from the below url, https://tempstore.myshopify.com/products/1234.json Its giving me 404 error. Although, I am able to access all products as below: https://tempstore.myshopify.com/products.json I have to access the product which was just processed in checkout process. I have its id as below: var products = Shopify.checkout.line_items; products will contain an array of product id's only which are processed in checkout.Now I need to access all other properties of these products. I can surely do this: https://tempstore.myshopify.com/admin/products/1234.json But it requires Authentication. Any thoughts?
From the frontend, you need to have the product handle to get the JSON object: https://tempstore.myshopify.com/products/[handle].js or https://tempstore.myshopify.com/products/[handle].json (Note that the returned values from the .js and .json endpoints are quite different from each other!) Like you point out, the Shopify.checkout.line_items array of objects only has the product IDs, not the product handles. We're not completely out-of-luck, though, because we can get the entire list of products in the store including the product handles by hitting the /products.json endpoint. Of course, this means grabbing a potentially huge JSON object just to get information that we should've had included in the checkout line items... but unless there's some alternate source of the line item information available on the checkout page, looping through the entire list may be what you need to do. So your end code would look something like this: Checkout.jQuery.getJSON( // Or whatever your preferred way of getting info is 'https://tempstore.myshopify.com/products.json', function(prodlist){ for(var p = 0; p < prodlist.length; p++){ var prod = prodlist[p]; // Find if Shopify.checkout.line_items contains prod.id, left as exercise for the reader if(found){ Checkout.jQuery.getJSON( 'https://tempstore.myshopify.com/products/' + prod.handle + '.js', function(product){ /* Whatever needs to be done */ }) } } } ) Hope this helps!
var shop = Shopify.shop; var lineItems = Shopify.checkout.line_items; var url = 'https://' + shop + '/products.json?callback=?'; var requiredData = []; $.getJSON(url).done(function(data){ lineItems.forEach(function(lineItemProduct){ data.products.find(function(product){ if(lineItemProduct.product_id == product.id){ requiredData.push(product); } }); }); }); console.log(requiredData); This is how I solved it, If it helps anybody :)
Create a drop down from an xml file, with no repeated values. Do I need an array?
I am still learning javascript and xml and have recently been presented with an issue I'm sure is simple to solve. I'm hoping for some help on this if possible. I have an xml file that is found here http://mrblesid.com/blueprint/bookinglist.xml I'm currently using this code to create a drop down list featuring the values from just one of the attributes "strArtistName" $(document).ready(function(artists){ $.ajax({ type: "GET", url: "bookinglist.xml", dataType: "xml", success: function(artists_list) { var select = $('#mySelect'); $(artists_list).find('vw_ADM_BookingListNull[strArtistName]').each(function(){ var artists = $(this).attr('strArtistName'); select.append('<option value="'+artists+'">'+artists+'</option>'); }); select.children(":first").text("please make a selection").attr("selected",true); } }); }); This is then called into a dropdown via the following <form> <select id="mySelect"> <option>loading</option> </select> </form> I would like to avoid repeating the artist names that are found for every entry, am I right to think I would need to use an array for this? If so how do I go about it? The name selected from the list should populate a variable to use elsewhere in the report. Any help would be greatly appreciates as I have deadlines looming. Thanks in advance, Mikey
An array will work. Update the main part to var artistsArr = []; $(artists_list).find('vw_ADM_BookingListNull[strArtistName]').each(function(){ var artists = $(this).attr('strArtistName'); if ($.inArray(artists, artistsArr) == -1) { select.append('<option value="'+artists+'">'+artists+'</option>'); artistsArr.push(artists); } }); Some browsers don't support Array.indexOf, so you can use jQuery's inArray.
First off, you should batch the DOM insertions for performance reasons (You can also squeeze a little more performance out by using an array instead of pure string concatanation) Here is your success function with some performance optimizations as well as the check for duplicate artists: function(artists_list) { var select = $('#mySelect'), html = [], artistArray = []; $(artists_list).find('vw_ADM_BookingListNull[strArtistName]').each(function(){ var artists = $(this).attr('strArtistName'); // Check the artistArray for a duplicate. // This will only work in more recent browsers. if (artistArray.indexOf(artists) === -1) { html.push('<option value="'+artists+'">'+artists+'</option>'); artistArray.push(artists); } }); // Join the HTML array and add it to the select element select.append(html.join('')) .children(":first").text("please make a selection").attr("selected",true); }
Too much JSON crashes browser :(
I have an ajax country/city selector. When I select United States the browser crashes. Doh! I have a dropdown list of countries. When I select a country a jQuery ajax call is run which gets a JSON response of cities belonging to that country. I should have seen it coming when I had to increase my allowed memory during execution. Here's the JSON response from selecting the UK. {"5947":"Aberdeen","12838":"Aberystwyth","15707":"Aldershot","18575":"Alsagers Bank","18682":"Altrincham","4863":"Andover","41802":"AOL","6471":"Armagh","18945":"Ascot","4864":"Ashby-de-la-Zouch","4865":"Ashford","5948":"Aviemore","12985":"Aylesbury","12281":"Ballymena","14446":"Banbury","12445":"Bangor","13631":"Barking","4866":"Barnet","17004":"Barnsley","16423":"Barrow-in-Furness","16254":"Basildon","12402":"Basingstoke","5826":"Bath","13289":"Beddgelert","15082":"Bedford","4868":"Belfast","4869":"Belper","13874":"Benfleet","5827":"Benson","15514":"Berkhamsted","4870":"Berwick Upon Tweed","12948":"Betws-y-Coed","18776":"Bexley","14530":"Bicester","4871":"Billericay","18436":"Birkenhead","4872":"Birmingham","14592":"Blackburn","14686":"Blackpool","12526":"Bolton","12480":"Bournemouth","13062":"Bracknell","18772":"Bradford","4873":"Braemar","4874":"Brecon","4875":"Brentwood","18820":"Brighton","14260":"Bristol","4876":"Broomfield","42004":"Burgess Hill","14654":"Burnley","4877":"Burton Upon Tren","13812":"Bury","15835":"Bury St Edmunds","16500":"Camberley","4878":"Cambridge","4879":"Canterbury","5957":"Cardiff","14443":"Carlisle","14065":"Carrickfergus","42384":"Chalgrove","5832":"Chatham","13641":"Chelmsford","4880":"Cheltenham","4881":"Chester","42879":"Chesterfield","12160":"Chichester","41768":"Chorley","14056":"Church Stretton","5949":"Cladich","4884":"Colchester","16204":"Congleton","17534":"Coniston","42888":"Corsham","4885":"Coventry","13575":"Crawley","15410":"Crewe","13913":"Croydon","4886":"Cumbernauld","13711":"Dartford","4887":"Dartmouth","5833":"Derby","17468":"Derry","4889":"Doncaster","13696":"Dorchester","15377":"Dorking","5834":"Dover","16659":"Dudley","41867":"Dumbarton","18091":"Dumfries","4890":"Dunbar","14217":"Dunblane","4891":"Dundee","14067":"Dunfermline","4892":"Durham","16058":"East Molesey","17521":"East Preston","12501":"Eastbourne","12374":"Eastrea","4893":"Edinburgh","18992":"Elgin","41763":"Ellesmere","12883":"Ely","16825":"Enfield","14510":"Epsom","5835":"Exeter","4894":"Falkirk","5836":"Falmouth","42388":"Faringdon","42034":"Farmington","14604":"Farnham","42347":"Feltham","12829":"Fleet","4895":"Forres","42315":"Frosterley","5950":"Glasgow","4896":"Glastonbury","12562":"Gloucester","15956":"Gosport","4898":"Grangemouth","12626":"Gravesend","16057":"Grays","4899":"Great Wilbraham","4900":"Greenock","12752":"Grimsby","11747":"Guildford","14506":"Guilford","11938":"Halifax","5010":"Hamilton","15553":"Harlow","41733":"Harpenden","14713":"Harrow","4902":"Hartlepool","18952":"Haslemere","13977":"Hastings","14917":"Hatfield","12529":"Haverfordwest","4903":"Haverhill","4904":"Hawarden","5951":"Hawick","11776":"Hemel Hempstead","15302":"Hereford","14999":"Hertford","41893":"Heston","16056":"Hexham","13019":"High Wycombe","13643":"Hoddesdon","5958":"Holyhead","12420":"Hornchurch","14160":"Horsham","12108":"Huddersfield","5837":"Hull End","13296":"Huntingdon","14801":"Hyde","17707":"Ilford","41721":"Inverkeithing","4905":"Inverness","5838":"Ipswich","4906":"Keighley","4907":"Kelso","18628":"Kendal","17805":"Kenilworth","4908":"Kennet","4909":"Kettering","18578":"Kidsgrove","18984":"Kilmarnock","4910":"Kingston Upon Hull","5952":"Kirkwall","18257":"Lakenheath","15425":"Lampeter","13182":"Lancaster","4911":"Laughton","13488":"Leamington","18824":"Leeds","13135":"Leek","17849":"Leicester","17716":"Leigh","12836":"Lerwick","13387":"Letchworth","4912":"Lewes","41767":"Leyland","13546":"Lichfield","5840":"Lincoln","19039":"Little Chalfont","16778":"Liverpool","13442":"Llandrindod Well","5953":"Loch Ness","12008":"London","15035":"Loughborough","15518":"Loughgall","15011":"Louth","18492":"Lowestoft","14023":"Luton","4913":"Machynlleth","12416":"Maidenhead","12230":"Maidstone","14722":"Manchester","4914":"Mansfield","4915":"Margate","4916":"Marlborough","17889":"Marlow","18870":"Melborne","16170":"Melton Mowbray","4917":"Merton","5844":"Middlesbrough","5959":"Milford","15181":"Millom","12315":"Milton Keynes","12089":"Mold","18816":"Montrose","5954":"Motherwell","18574":"Nantwich","4918":"Newark","17097":"Newbury","5845":"Newcastle","4919":"Newcastle Upon Tyne","19040":"Newport","41682":"Newquay","13629":"Northallerton","4922":"Northampton","18577":"Northwich","42209":"northwold","15080":"Norwich","5847":"Nottingham","4923":"Oban","11975":"Oldham","6474":"Omagh","17161":"Oxford","15422":"Oxshott","18627":"Penrith","4925":"Penzance","16404":"Perth","5848":"Peterborough","4926":"Plains","4927":"Plymouth","15551":"Pontypridd","14208":"Poole","4928":"Portsmouth","17642":"Portstewart","41766":"Preston","5011":"Prestwick","18579":"Radway Green","42069":"Ramsgate","11775":"Reading","14706":"Redditch","16276":"Ringwood","15522":"Ripon","14673":"Rochester","15968":"Romford","41857":"Rugby","15289":"Runcorn","17520":"Rustington","14052":"Saint Albans","16462":"Salford","4931":"Salisbury","42295":"Sandwich","17690":"Sandy","4932":"Scarborough","13975":"Seaford","12003":"Shaftesbury","18891":"Sheffield","5850":"Shrewsbury","13178":"Slough","14708":"Solihull","4935":"Southampton","4936":"Southborough","14524":"Southend-on-Sea","13970":"Southport","42260":"St Albans","5955":"St Andrews","15841":"St Asaph","18576":"St Helens","16114":"St Ives","12717":"Stafford","41746":"Staines","14051":"Stanmore","16656":"Stansted","42032":"Stevenage","5012":"Stirling","11801":"Stockport","14198":"Stockton-on-Tees","4937":"Stoke On Trent","42386":"Stranraer","4938":"Stratford-Upon-Avon","4939":"Stroud","18615":"Sudbury","11860":"Sunderland","16393":"Sutton","5960":"Swansea","12853":"Swindon","4941":"Taunton","5851":"Teeside","13973":"Telford","4943":"Truro","17702":"Virginia Water","5852":"Waddington","12059":"Wakefield","4945":"Wallingford","4947":"Wareham","5853":"Warrington","4948":"Warwick","4949":"Watford","12009":"Wellingborough","12528":"Wellington","13366":"Wells","12530":"Welwyn Garden City","16785":"Weston Under Lizard","16334":"Wetherby","18171":"Weymouth","4950":"Whitby","13308":"Whitehaven","42387":"Whitehead","5956":"Wick","17581":"Wilmslow","5854":"Wimbledon","12524":"Wimborne Minster","12551":"Winchester","15946":"Windsor","18573":"Winsford","4952":"Wisbech","4953":"Wisborough Green","12982":"Woking","18769":"Wokingham","13287":"Wolverhampton","17904":"Woodford","18086":"Woolavington","11783":"Worcester","12128":"Worthing","5961":"Wrexham","13630":"Yarm","17015":"Yeovil","11824":"York"} Here is my Javascipt: $('#current-country').change(function(){ //any select change on the dropdown with id country trigger this code $('.select-current-city').show(); $("#current-cities > option").remove(); //first of all clear select items var country_id = $('#current-country').val(); // here we are taking country id of the selected one. $.ajax({ type: "POST", url: "<?php echo base_url()?>map/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id success: function(cities) //we're calling the response json array 'cities' { $.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value { var opt = $('<option />'); // here we're creating a new select option with for each city opt.val(id); opt.text(city); $('#current-cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities' }); } }); }); Has anyone any suggestions on how I can process this much data in the browser? I'm using PHP (Codeigniter), MySQL and jQuery.
I would recommend creating an array of your new option nodes, and then appending them en masse. Doing them one at a time may be what's killing you. var newOptions = []; $.each(cities,function(id,city) { var opt = $('<option />', { "val": id, "text": city }); newOptions.push(opt[0]); //need to push actual dom node - thanks RightSaidFred }); $('#current-cities').append(newOptions); Or should this be clearing previous options in the dropdown? If so: $('#current-cities').html(newOptions);
First double check you are not making the same AJAX request more than once. A quick and easy solution would be to split your AJAX requests in more than one. Start by dividing it into two, and if that still isn't enough, divide them by 3 or more. You can then check which JSON size is the right one for you, and use that one. Or you can have your php determine how much requests are needed based on the number of total cities. For really large lists, I had to split the requests into 100 items each. The first request I would get the first piece of list along with a bit of data indicating how much requests I had to make to obtain the full list, append the new nodes, then I would make the remaining requests, until I got the full list.
I don't see how that would crash for you. Here I am doing the worst possible thing and it is blazing fast: http://jsfiddle.net/HZnYQ/ Each time you select something, I remove all elements one by one and then append them one by one, and it's still instant. Actually my CPU doesn't even make a note of it.