mrmarkfrench / country-select-js

A quick jQuery-based country picker based on https://github.com/Bluefieldscom/intl-tel-input.git
MIT License
282 stars 105 forks source link

Initialize countrySelect on new elements added in a ajax request is not existing #68

Closed fede72bari closed 4 years ago

fede72bari commented 4 years ago

I am experiencing that I cannot find the function .countrySelect in new elements added within an ajax request since the function really does not exist in new #countryN input tags. I thought it was a problem of scope, after exhausting attempts, at least for a beginner like me, I found that probably there is an event linked to the page load or ready so that just at the first-page load the whole countrySelect.js script attach the plugin function to many page elements including input tags. So my question is how can i get the same for the new elements that are added basically after clicking a SHOW MORE button? Is there a way to recall the function inside the countrySelect.js script that attaches the plugin countrySelect function? Or a way to trigger the event load/ready page? Thanks.

mrmarkfrench commented 4 years ago

So I just tried this and it's working as expected for me.

Here's my dead-simple example. I took the demo.html file from the plugin and added the following to the end:

<script>
    // Make sure we don't run this until after everything else has been loaded.
    // Using the document click handler ensures that everything else is setup first,
    // which ensures we're not cheating and adding our new element before the
    // plugin attaches to form elements the first time.
    $(document).on('click', function(){
        // Step 1: Create a new element from a simulated AJAX request
        var newInput = $('<input type="text" id="country_selector_ajax" name="country_selector_ajax">');
        // Step 2: Add the new element to the page
        $('form').append(newInput);
        // Step 3: Run the plugin on the new element
        $(newInput).countrySelect({ defaultCountry: 'au' });
        // Step 4: Profit!
    });
</script>

This will add a new selector every time you click on the page, and they all get a handler added to them with a drop-down menu of flags.

fede72bari commented 4 years ago

Dear Mark,

thank you so much for your reply. I have repeated your test also in the context of ajax call and keeping it simple; in this way i have to say that works it here too. But still, I have that trouble in the page I need to complete and I am driving crazy because the php function that adds new elements is the same that create the first ten as well as the js functions and event that manage the new ones are the same used for the first ones. So no reason for this difference of behavior apparently. Giving a look to the input objects on which I apply countrySelect and logging them in the console here in the attached image there are the two differences, on the left side one of the first 10 elements in main page response, on the right one of the elements added through ajax call. If you have any idea please tell me I cannot see anymore workaround to understand the origin of the problem and have been close at home for covid-19 for 5 weeks ... i don't have any more walls on which beat my head! :-) www.pharmacomparison.com --> search by "aspirin" --> scroll down, "show more".

Thanks.

f

The working attemps:

controller:

viewer

*<!DOCTYPE html> Country Select JS <link rel="stylesheet" href="build/css/demo.css"> <link rel="stylesheet" href="<?php echo base_url('assets/css/countrySelect.css'); ?>"> <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery.js"> <script type="text/javascript" src="<?php echo base_url('assets/js/countrySelect.min.js'); ?>">

Country Select JS

<div class="" id="country"> <label for="country_selector" style="display:none;">Select a country here...
<button type="submit" style="display:none;">Submit <button type="submit" id="local_test">Local Test <button type="submit" id="ajax_test">Ajax Test *

Il giorno dom 12 apr 2020 alle ore 08:19 Mark French < notifications@github.com> ha scritto:

So I just tried this and it's working as expected for me.

Here's my dead-simple example. I took the demo.html file from the plugin and added the following to the end:

This will add a new selector every time you click on the page, and they all get a handler added to them with a drop-down menu of flags.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mrmarkfrench/country-select-js/issues/68#issuecomment-612570373, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALDW2SZGKDCGNXCFSQAR6VLRMFMQHANCNFSM4MGFXN2A .