CodeIgniter Forums
Ajax Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Ajax Question (/showthread.php?tid=67685)



Ajax Question - wolfgang1983 - 03-25-2017

When I keyup on my input it runs the ajax

But on success it creates a class called well multiple times as shown in image below

[Image: 3GlIXGdE7aUN.png]



How can I make sure it only creates one class only even when keyup.

Code:
$('input[name=\'tags\']').on('keyup', function(e){
    $.ajax({
        url: "<?php echo base_url('questions/tags');?>",
        dataType: 'json',
        type: 'post',
        data: {
            tags: $('input[name=\'tags\']').val()
        },
        success: function(json) {
            $('<p class="well"></p>').insertAfter($("input[name='tags']"));
        }
    });

});



RE: Ajax Question - Wouter60 - 03-25-2017

Replace
Code:
on('keyup'

with:
Code:
one('keyup'



RE: Ajax Question - wolfgang1983 - 03-25-2017

(03-25-2017, 02:36 AM)Wouter60 Wrote: Replace
Code:
on('keyup'

with:
Code:
one('keyup'

Is it possible to still use on?


RE: Ajax Question - Wouter60 - 03-25-2017

I think I don't understand your problem.
You are using on('keyup'), which fires the ajax routine every time you release a key on your keyboard. So when you type 'city', it is fired 4 times.
If you use one('keyup'), the same happens, but only once. So the <p class="well"></p> will be inserted after your input just once.