Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conflict between jquery and bootstrap

I have a code where I am including jquery files and bootstrap files in the header.php. I am running into issues where if I include the jquery file before bootstrap.js file, it messes up the other tabs on my webpage and basically even if i click on the other tabs it does not navigate me.

I think there is a conflict between jquery and bootstrap. I am pasting my header.php file below for more reference.

header.php

<?php require_once "essentials.php";
//error_reporting(~0);
//ini_set('display_errors', 1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<style>{height:150px; width:1300px;}</style>-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="Public">
<title><?php echo $page_title?></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script src="script_dbafactory.js?<?php echo rand(0,511);?>"></script>
<link href="css/bootstrap.css" rel="stylesheet">
<!--<link href="css/style_header.css" rel="stylesheet">-->
</head>
<body>
<div class="container">
        <div class="row clearfix">
                <div class="col-md-4 column">
                        <img alt="logo" src="./images/source.png">
                </div>
                <div class="col-md-8 column dbf">
                        <h2>
                                DBA Factory
                        </h2>
                </div>
        </div>
        <div class="row clearfix">
                <div class="col-md-12 column">
                        <div class="tabbable" id="tabs-775712">
                                <ul class="nav nav-tabs">
                                         <li>
                                                <a href="../dbafactory/home_page.php?sub=1" data-toggle="tab">Home</a>
                                        </li>
                                        <li>
                                                <a href="../dbafactory/form_page.php?sub=2" data-toggle="tab">Submit a project</a>
                                        </li>
                                        <li>
                                                <a href="../dbafactory/view_table.php?sub=3" data-toggle="tab">All Projects</a>
                                        </li>
                                        <li>
                                                <a href="#panel-4" data-toggle="tab">Innovative Ideas</a>
                                        </li>
                                        <li>
                                                <a href="#panel-5" data-toggle="tab">Matchmaker</a>
                                        </li>
                                        <li>
          <a href="#panel-6" data-toggle="tab">Meet the Team</a>
                                        </li>

                                        <div class="dropdown">
                                        <?php include "userlogin.php"; ?>
                                        </div>
                                </ul>

                        </div>
                </div>
        </div>


</div>
</body>

<!--<script type="text/javascript">
//Script to implement tabs 
$('#myTab a').click(function (e) {
  e.preventDefault()
  $(this).tab('show')
})

</script>-->
<script>
$.noConflict();
$(document).ready(function (){
        $('.dropdown-toggle').dropdown();
        $('#userlogin').dropdown('toggle');
});
</script>

Can someone please let me know how do i solve this issue? and how should i load all the js and css files to avoid any issues with the webpage.

Thanks

like image 462
xyzzz Avatar asked Feb 06 '14 02:02

xyzzz


People also ask

Can you use jQuery and bootstrap together?

Bootstrap 5 is designed to be used without jQuery, but it's still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it'll add all of our components in jQuery's plugin system; this means you'll be able to do $('[data-bs-toggle="tooltip"]'). tooltip() to enable tooltips.

Why did bootstrap stop using jQuery?

The Bootstrap 5 framework has removed jQuery as a requirement. It saved 85KB of minified JavaScript, which could be significant as Google starts to use page speed as a ranking factor for mobile web sites, and soon for desktop web sites as well.

What is the difference between jQuery and bootstrap?

Using Bootstrap you can develop a feature that you can work in with every size of the screen as it provides consistency across all the browsers and different devices, Whereas, JQuery UI is used for the development of an user interface based on HTML to make the websites more responsive on all the browsers.

Is bootstrap is an alternative to jQuery?

Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web; jQuery: The Write Less, Do More, JavaScript Library. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML.


2 Answers

data-toggle="tab" for bootstrap means there has to have a [tab panel] for it, however, you only use it as nav, and it caused the problem.

Please read: http://getbootstrap.com/javascript/#tabs

Also, use js closure can more easier to avoid js conflict issue:

(function($){
    ....
})(jQuery);

Please check the code below, you can use WinMerge to compare the code with your own:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--<style>{height:150px; width:1300px;}</style>-->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="Public">
<title><?php echo $page_title ?></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" type="text/javascript"></script>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet">
<!--<link href="css/style_header.css" rel="stylesheet">-->
</head>
<body>
<div class="container">
        <div class="row clearfix">
                <div class="col-md-4 column">
                </div>
                <div class="col-md-8 column dbf">
                        <h2>
                                DBA Factory
                        </h2>
                </div>
        </div>
        <div class="row clearfix">
                <div class="col-md-12 column">
                        <div class="tabbable" id="tabs-775712">
                                <ul class="nav nav-tabs">
                                         <li>
                                                <a href="#panel-4">Home</a>
                                        </li>
                                        <li>
                                                <a href="#panel-4">Submit a project</a>
                                        </li>
                                        <li>
                                                <a href="#panel-4">All Projects</a>
                                        </li>
                                        <li>
                                                <a href="#panel-4">Innovative Ideas</a>
                                        </li>
                                        <li>
                                                <a href="#panel-5">Matchmaker</a>
                                        </li>
                                        <li>
                                                <a href="#panel-6">Meet the Team</a>
                                        </li>

                                        <li class="dropdown">
                                            <a id="userlogin" role="button" data-toggle="dropdown" href="#">rdesai<span class="caret"</span></a>

                                            <ul class="dropdown-menu" role="menu" aria-labelledby="userlogin">
                                                <li role="presentation"><a role="menuitem" tabindex="-1" href="#">Settings</a></li>
                                            </ul>
                                        </li>
                                </ul>

                        </div>
                </div>
        </div>


</div>
<script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script>
<script>
(function($){

    $(document).ready(function (){
            $('.dropdown-toggle').dropdown();
            $('#userlogin').dropdown('toggle');
    });

})(jQuery);
</script>
</body>
</html>
like image 93
Zac Avatar answered Sep 30 '22 10:09

Zac


Usually when i run into jquery and bootstrap conflicts, i just add this as the first line of my php file:

<script>jQuery.noConflict();</script>

This is to avoid conflicts with other libraries.

You can read more here: https://api.jquery.com/jQuery.noConflict/

Edit:

Or use

<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.min.js" type="text/javascript"></script>

instead of 1.10.3

like image 41
user3221743 Avatar answered Sep 30 '22 10:09

user3221743