I have a page using jQuery and Bootstrap. Everything works fine, but for some reason, when I call a function on an event the console says $(...).function is not a function
. For example, if I have this:
<script>
$("#myModal").modal('show');
</script>
It works fine, and the modal shows as soon as you load the page. However, if I try to call it like this:
<script>
$("#myTrigger").click(function(e){
e.preventDefault();
$("#myModal").modal('show');
});
</script>
It doesn't work and the console gives this error:
$(...).modal is not a function
Any ideas?
EDIT:
<script>
$(document).ready(function(){
$("#myModal").modal('show');
});
</script>
works fine, but again,
<script>
$(document).ready(function(){
$("#myTrigger").click(function(e){
e.preventDefault();
alert("test); // Does alert
$("#myModal").modal('show'); // Doesn't work
});
});
</script>
...doesn't. Since the alert worked, it's safe to say that the trigger does exist and the click event is being called properly. I'm using a couple other frameworks, a WYSIWYG editor and uploadify. Could this be a conflict error? I didn't think so at first since both cases are being tried right after each other, so if there was a conflict it wouldn't work on either, right?
UPDATE:
Looks like it was a pretty darn weird conflict issue. I tried the following:
var bootstrapModal = $.fn.modal.noConflict();
$.fn.bModal = bootstrapModal; // established outside the click event
$("#myTrigger").click(function(e){
e.preventDefault();
$("#myModal").bModal('show'); // doesn't work
});
And I still got the same issue. However, establishing $.fn.bModal
inside the click event, as so:
var bootstrapModal = $.fn.modal.noConflict();
$("#myTrigger").click(function(e){
e.preventDefault();
$.fn.bModal = bootstrapModal; // established inside the click event
$("#myModal").bModal('show'); // works fine
});
Worked. Still can't wrap my head around why this happened and why it worked on $(document).ready()
but not on $('#trigger').click()
, maybe the specific way Chrome deals with conflicts was also part of it. Anyway, this will do. Thanks to everyone who helped.
` to ensure the DOM has been loaded, is that correct?
– Rory McCrossan Jan 04 '17 at 16:09