How to load a html file into div tabs using javascript - javascript

I have created nav-tabs using bootstrap. Now I want to load html files into tab content on tab click. Can someone help me with java script.
This is my html:
<body>
<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<ul class="nav nav-pills nav-stacked">
<li class="active"><a data-toggle="pill" id="Home" href="#home">Home</a></li>
<li><a data-toggle="pill" id="Menu1" href="#menu1">Menu 1</a></li>
<li><a data-toggle="pill" id="Menu2" href="#menu2">Menu 2</a></li>
</ul>
</div>
<div class="col-sm-9">
<div class="tab-content">
<div id="home" class="tab-pane fade in active">
</div>
<div id="menu1" class="tab-pane fade"> <h3>Menu 1</h3>
</div>
<div id="menu2" class="tab-pane fade"> <h3>Menu 2</h3>
</div>
</div>
</div>
</div>
</div>
</body>
In the above code, when i click on the home tab i want to load an external html(x.html) file into home div under tab-content div. I want the same functionality when i click on the menu1 tab(y.html). When I load (y.html) i need to hide (x.html). Someone please help me...!!!
I used the below javascript. But it is not working.:
$('a#Home').click(function(){
$("#home").load("x.html");
});
$('a#Menu1').click(function(){
$("#menu1").load("y.html");
});

You can use .hide(), .show()
$('a#Home').click(function(e) {
e.preventDefault();
$("#home").load("x.html", function() {
$("#menu1").hide();
$(this).show();
});
});
$('a#Menu1').click(function(e) {
e.preventDefault();
$("#menu1").load("y.html", function() {
$("#home").hide();
$(this).show();
})
});

Related

Change Bootstrap JS Tab

I have the following script:
$(".btn.btn-default").click(function () {
$(this).siblings().removeClass('active').end().addClass('active').val($(this).text());
if (this.id.contains('fear')) {
$('#fear').val($(this).text());
} else if (this.id.contains('control')) {
$('#control').val($(this).text());
} else if (this.id.contains('danger')) {
$('#danger').val($(this).text());
$('#tab2').tab('show');
}
});
Focusing specifically on when #danger is clicked, I'm trying to change the bootstrap tab with:
$('#tab2').tab('show');
however it does not seem to work. Any ideas?
//Updated with tab html
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#tab1">Step 1</a></li>
<li><a data-toggle="tab" href="#tab2">Step 2</a></li>
</ul>
<div class="tab-content">
<div id="tab1" class="tab-pane fade in active">
<div id="obj" class="draggable drag-drop"> Grass </div>
<div id="obj" class="draggable drag-drop"> Tree </div>
</div>
<div id="tab2" class="tab-pane fade in active">
<div id="act" class="draggable drag-drop"> Fire </div>
<div id="act" class="draggable drag-drop"> Collapsed </div>
</div>

Bootstrap Tabs Not Active nor Showing Content

I am working on a personal website and I am running into a bootstrap err. It seems pretty common and I cant seem to fix it with anything I have read up on. I have the following code and any help is, well... helpful.
$(document).ready(function() {
"use strict";
activaTab('about');
});
function activaTab(tab) {
"use strict";
$('.nav-tabs a[href="#' + tab + '"]').tab('show');
}
<div class="tabs">
<nav>
<ul class="nav nav-tabs nav-justified">
<li class="active"><a data-toggle="tab" href="#about">About</a>
</li>
<li><a data-toggle="tab" href="#work">Work</a>
</li>
<li><a data-toggle="tab" href="#school">School</a>
</li>
<li><a data-toggle="tab" href="#nifty">Other Nifty Items</a>
</li>
</ul>
</nav>
<div class="tab-content" id="tabs">
<div id="about" class="tab-pane fade in active">
le content
</div>
<div id="work" class="tab-pane fade">
le content
</div>
<div id="school" class="tab-pane fade">
le content
</div>
<div id="nifty" class="tab-pane fade">
le content
</div>
</div>

Bootstrap 3 data-target attribute on tab not working

I am using Bootstrap tabs in my website. When page loads tab will be filled with data at run-time using dynamic content. I am using data-target attribute on anchor tag for that, but it does not seems to work at all.
Here is what I tried so far:
HTML
<div class="container">
<div class="row">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
Tab 1
</li>
<li role="presentation">
Tab 2
</li>
<li role="presentation">
Tab 3
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="test_1"></div>
<div role="tabpanel" class="tab-pane active" id="test_2"></div>
<div role="tabpanel" class="tab-pane active" id="test_3"></div>
</div>
</div>
</div>
Here I am trying to load content of remote file showTab1.do inside div with id="test_1" when using data-target="#test_1" on anchor tag. But it is not working. What I know is if I am using data-target we don't need to write any jQuery/JavaScript to load data in tab as data-target will do that for us(correct me if I am wrong).
My Bootstrap version is v3.3.6.
Please correct me where I am wrong.
Thanks
There is no way to automatically load data from another url into the tab content using data attributes. You'd need to fire the jQuery load() method when the show tab event fires...
$('a[data-toggle="tab"]').on('show.bs.tab', function (e) {
...
$(target).load(url,function(result){
...
});
});
Example:
http://codeply.com/go/xZfTnAtKUM
HTML :
<div class="container">
<div class="row">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
Tab 1
</li>
<li role="presentation">
Tab 2
</li>
<li role="presentation">
Tab 3
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="test_1"></div>
<div role="tabpanel" class="tab-pane" id="test_2"></div>
<div role="tabpanel" class="tab-pane" id="test_3"></div>
</div>
</div>
</div>
and load the tab content using jQuery load
JAVASCRIPT:
<script>
$(document).ready(function () {
$('#test_1').load('showTab1.do');
$('#test_2').load('showTab2.do');
$('#test_3').load('showTab3.do');
});
</script>
There were a couple of errors but this works. I added the Tab 1,2 and 3 to the tab panes so you can see the result when you click the nav links (jsut remove the text so it doesn't mess up your flow)..
<div class="container">
<div class="row">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">Tab 1</li>
<li role="presentation">Tab 2</li>
<li role="presentation">Tab 3</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="test_1">Tab 1</div>
<div role="tabpanel" class="tab-pane fade" id="test_2">Tab 2</div>
<div role="tabpanel" class="tab-pane fade" id="test_3">Tab 3</div>
</div>
</div>
</div>
Hope this helps - and then load the data into the tab panes using the .load() method as described below.

Dynamically created tabs not showing after creation in Bootstrap

I'm just using a button now to try and get it to work. It is supposed to make another selection in the dropdown box which it does, then show the content page. However it isn't changing the display to the page that was created and instead I have to manually click the new selection in the dropdown.
HTML:
<ul class="nav nav-tabs" id="srTabs">
<li class="active"><a data-toggle="tab" href="#home">Home</a></li>
<li><a data-toggle="tab" href="#search">Search</a></li>
<li><a data-toggle="tab" href="#upload">Upload</a></li>
<li><a data-toggle="tab" href="#notes">Notes</a></li>
<li class="dropdown">
Submissions <i class="fa fa-caret-down"></i>
<ul class="dropdown-menu" id="submissionContainer">
<li>Grid</li>
</ul>
</li>
<li><a data-toggle="tab" href="#history">History</a></li>
</ul>
<div class="tab-content" id="srContent">
<div id="home" class="tab-pane fade in active">
<div id="navigationBar" class="text-center">
<button onclick="makeSub(150)">Test</button>
</div>
</div>
<div id="search" class="tab-pane fade"></div>
<div id="upload" class="tab-pane fade"></div>
<div id="notes" class="tab-pane fade"></div>
<div id="grid" class="tab-pane fade">
<h1 align="center">Submissions</h1>
</div>
<div id="history" class="tab-pane fade">
</div>
</div>
JavaScript
function makeSub(id) {
$('<li>'+id+'</li>').appendTo('#submissionContainer');
$('<div id="sub'+id+'" class="tab-pane fade"></div>').appendTo('#srContent');
$('#sub'+id).load('/webapp/submissions/' + id);
$('#sub'+id).tab('show');
}
You either have to bind click event to the button like in this fiddle I made, or move your makeSub function outside of the $.ready function where button's onclick event can find the function.
$.ready(function(){
//everything else
});
function makeSub(id) {
$('<li>'+id+'</li>').appendTo('#submissionContainer');
$('<div id="sub'+id+'" class="tab-pane fade"></div>').appendTo('#srContent');
$('#sub'+id).load('/webapp/submissions/' + id);
$('#sub'+id).tab('show');
}

loading new page in tabs using twitter bootstrap

Hi I am using the following code to create tabbed layout using twitter bootstrap
<ul class="nav nav-tabs" >
<li class="active">Request Audit</li>
<li>status</li>
<li>Settings</li>
<li>Help</li>
</ul>
now in all tabs i need different page to load.So i want to do this :
<ul class="nav nav-tabs" >
<li class="active">Home</li>
<li>status</li>
<li>Settings</li>
<li>Help</li>
</ul>
I don't want to load the content from same page in all tabs and above code is not loading new page.Please help..
update :
<ul class="nav nav-tabs" id="myTab">
<li class="active">Request Audit</li>
<li>status</li>
<li>Settings</li>
<li>Help</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="audit">
<p>audit</p>
</div>
<div class="tab-pane" id="status">
<p>status</p>
</div>
<div class="tab-pane" id="settings">
<p>settings</p>
</div>
<div class="tab-pane" id="help">
<p>help</p>
</div>
</div>
I have added this script
<script>
$('#myTab a[href="#status"]').click(function (e) {
e.preventDefault();
$(this).tab('show');
$('#status').load('status.html');
});
</script>
There are several ways to do this, but before deciding which one to use, I'd want to make sure you actually want to piece out your files like that. What's keeping you from just putting the content inside of the tabs on the page as it is?
If not, why not use PHP?
If you decide to stick with your current plan, the way I'd accomplish what you're looking to do would be through jQuery and AJAX.
First, you want to make sure you're using the proper Bootstrap structure – check out the Tabbable Nav section, and make your markup tabs correspond to empty content areas:
<div class="tabbable" style="margin-bottom: 18px;">
<ul class="nav nav-tabs">
<li class="active">Request Audit</li>
<li class="">Status</li>
<li class="">Settings</li>
<li class="">Help</li>
</ul>
<div class="tab-content" style="padding-bottom: 9px; border-bottom: 1px solid #ddd;">
<div class="tab-pane active" id="tab1">
<p>Placeholder 1</p>
</div>
<div class="tab-pane" id="tab2">
<p>Placeholder 2</p>
</div>
<div class="tab-pane" id="tab3">
<p>Placeholder 3</p>
</div>
<div class="tab-pane" id="tab4">
<p>Placeholder</p>
</div>
</div>
</div>​
Then, use jQuery.load to fill in the tab-panes!
$('#tab2').load('/status.html');
$('#tab3').load('/settings.html');
$('#tab4').load('/help.html');
Just remove the data-toggle="tab"
for example: on the Home Page:
<ul class="nav nav-tabs" >
<li class="active"><a href="#" >Home</a></li>
<li><a href="status.html" >status</a></li>
<li><a href="settings.html" >Settings</a></li>
<li><a href="help.html" >Help</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<p>Home Page content 1</p>
</div>
</div>​
On the status.html page:
<ul class="nav nav-tabs" >
<li ><a href="#" >Home</a></li>
<li class="active"><a href="status.html" >status</a></li>
<li><a href="settings.html" >Settings</a></li>
<li><a href="help.html" >Help</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<p>status content 1</p>
</div>
</div>​
The bad thing is just have some copy of the nav-tab codes

Categories

Resources