How to resolve jQuery conflict?


How to resolve jQuery conflict?



I have a jQuery custom file which is disabling or conflicting my other jQuery file, please how do i resolve this. Please see below:
My Custom file:


$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});



Conflicting Files:


<script src="js/jquery.isotope.min.js"></script>
<script src="js/nprogress.js"></script>



I would be more than happy if this is resolved.





Can't find any conflicting files. Are you sure?
– Praveen Kumar Purushothaman
May 4 '16 at 8:48





Are u useing two diffrent versions of Jquery??
– Venkata Krishna Reddy
May 4 '16 at 8:49





I just edited, the conflicting files are visible now
– Harrison Oziegbe
May 4 '16 at 8:51





And what do you mean by conflict? Is isotope not working as expected or nprogress one???
– A. Wolff
May 4 '16 at 8:55





I noticed the "keyup(function() " at the top of my custom file in $(".search").keyup(function() is exactly what is disabling the other files. When i remove the function(), the other files works fine but my custom file won't work and i want both to work as they do different thing. Any help please?
– Harrison Oziegbe
May 4 '16 at 8:56




2 Answers
2



I'm assuming that you know for certain jQuery is conflicting - you've checked the console errors, etc.



You could wrap your jQuery in a self-executing anonymous function as follows:


(function($) {
$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});
})(jQuery);



EDIT - a bit of explanation of what's happened above. In order to prevent any potential conflicts with other scripts / frameworks we are passing the jQuery object as an argument to our function (hence function($)). The benefits of doing this is that you can now use $ locally within the function at will. Without fear of conflicting with other scripts on a global scope.


function($)


$


global





Issue seems not related to mutliple included jQuery versions but related to ajax reloaded content
– A. Wolff
May 4 '16 at 9:00






@A.Wolff I think you're probably right. At the time of writing my original answer, I was assuming the OP had checked the console errors - hence why he said it was a standard multiple jQuery / script conflict.
– David Wilkinson
May 4 '16 at 9:04



I Was able to fix the problem by just wrapping it with:


$(document).ready(function() {
//-----
});






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

List of Kim Possible characters

Python Tkinter Error, “Too Early to Create Image”