I want to create something like tabs.
So I have tab itself as
<div class="tab" >Tab1</div>
<div class="tab" >Tab2</div>
<div class="tab" >Tab3</div>
and page content for each tab as
<div class="pagecontent" >pagecontent for Tab1</div>
<div class="pagecontent" >pagecontent for Tab2</div>
<div class="pagecontent" >pagecontent for Tab3</div>
How to make js function in "clever way" so by pressing "tab1", "pagecontent for Tab1" shows and other 2 get hidden? Also, number of tabs varies from page to page.
$(function() {
$('.pagecontent').not(':eq(0)').hide();
$('.tab').click(function() {
$('.pagecontent').hide();
$('.pagecontent').eq($(this).index()).show();
});
});
Please see a working demo here > http://jsfiddle.net/Yce32/
Try jQuery UI Tabs:
http://jqueryui.com/tabs/
It's "clever way" - you will have add\delete\order tabs in a box
Related
I am developing an web app. In some of the web pages I am using Twitter Bootstrap pane tabs. below is my code.
I am trying to go to a specific tab when the page reloads.
For example: when I am in the www.mywebsite.com/c.html the first active tab is 'bc' when I go to 'cc' and refresh the page it goes back to 'bc'. I want it to stay in 'cc'. I need to be able to do something like this www.mywebsite.com/c.html#cc But this does not work.
I looked at Twitter Bootstrap Tabs: Go to Specific Tab on Page Reload? but could not find an answer that fits my code. I tried changing the class names still no luck.
<div class="wp">
<div class="widget">
<div class="tabs">
<ul class="nav nav-pills nav-justified">
<li class="active">bc</li>
<li>cc</li>
<li>lc</li>
<li>pc</li>
<li>sc</li>
</ul>
</div>
</div>
</div>
<div class="tab-content tab-content-inverse">
<div class="tab-pane active" id="bc">
...
</div>
<div class="tab-pane" id="cc">
...
</div>
<div class="tab-pane" id="lc">
...
</div>
<div class="tab-pane" id="pc">
...
</div>
<div class="tab-pane" id="sc">
...
</div>
</div>
Get the anchor part of the URL by using location.hash in your JavaScript/jQuery.
Then, append it into the following JavaScript:
document.querySelector('.nav li a[href="#lc"]').click();
You'll simply insert the value you pull using location.hash after the href= portion, which will then be clicked.
Change the value in the following Bootply example and run it to see how it works.
BOOTPLY
This worked for me. I changed the class names to tabs and the event to click event
$(document).ready(function () {
var hash = document.location.hash;
var prefix = "tab_";
if (hash) {
$('.tabs a[href=' + hash.replace(prefix, "") + ']').tab('show');
}
$('.tabs a').on('click', function (e) {
window.location.hash = e.target.hash.replace("#", "#" + prefix);
});
});
You can write some code in JavaScript that will run when the page loads and check the # part of the url (window.location.hash). Take a look at the bootstrap website for how to toggle a certain tab: http://getbootstrap.com/javascript/#tabs
I am using jQuery to hide some content in collapsible panels, to do this I am using the following mark up and script:
HTML
<div class="trigger">Collapsible Header 1 - Click to toggle</div>
<div class="panel">
content 1
</div>
<div class="trigger">Collapsible Header 2 - Click to toggle</div>
<div class="panel">
content 2
</div>
jQuery:
$(document).ready(function() {
$(".trigger").click(function(){
$(this).next(".panel").slideToggle("medium");
});
});
A fiddle of it here:
http://jsfiddle.net/LkTf8/1/
This works great, however I am trying to start with the panels collapsed. Currently, they are open, not closed. What is the best approach to start with the panels collapsed?
set .panel to display: none
FIDDLE
I'm trying to fix the top menu on page change.
Example:
<div data-role="page" id="page1">
//page 1 contents
</div>
<div data-role="page" id="page2">
//page 2 contents
</div>
and my top menu
<div id="top-menu">
</div>
Is there any way to not make the menĂ¹ code reappear on each page?
I want my menu div to be collective for all pages.
Any ideas?
Thanks,
Andrea
No, the concept of "pages" does not allow for static content to be shared among them.
But I see at least two solutions:
use only one page containing your menu, and many hidden div to encapsulate your content (like <div data-role="content" id="page1" class="hidden">). Using your menu navigation, you just have to show/hide the content you need
use some JavaScript trickery:
$(document).on("pagebeforeshow", "[id^=page]", function(event)
{
// "move" the menu to the content of the current page
// (you have to add IDs to each page like 'page1content')
$("#top-menu").prependTo("#" + this.id + "content");
});
I'm using a twitter-bootstrap nav bar combined with tabs to load in forms via ajax (I'm using Django to serve the forms)
<ul class="nav nav-pills" id="tabs">
<li>Form1</li>
<li>Form2</li>
<li>Form3</li>
</ul>
<div class="tab-content">
<div class="tab-pane" id="form1" data-src="{% url form1 %}"></div>
<div class="tab-pane" id="form2" data-src="{% url form2 %}"></div>
<div class="tab-pane" id="form3" data-src="{% url form3 %}"></div>
</div>
This couples with the jquery:
$('#tabs').bind('show', function(element) {
paneID = $(element.target).attr('href');
src = $(paneID).attr('data-src');
$(paneID).load(src);
});
Firstly, part of me wonders whether this might be a bad idea. I can't think of a reason why but if someone could point out to me whether this method has any drawbacks I would appreciate it.
Secondly, When I navigate away from a tab, I'd like to trigger the "onunload" event for that tabs content. But I can't seem to find a way to "unload" the data from a tab when it is navigated away from.
$('#tabs').bind('hide', function(element) {
doStuff();
});
is not valid apparently. Any help/guidance appreciated.
Bootstrap tab fires an event when the tab is shown.
http://getbootstrap.com/2.3.2/javascript.html#tabs
Taken from bootstrap:
$('a[data-toggle="tab"]').on('shown', function (e) {
e.target // activated tab
e.relatedTarget // previous tab
})
You can use the relatedTarget. That is the tab that just "unloaded".
Hi and happy new year,
There is this JQM dialog, which opens likes this via an AJAX call
$('#calendar-event-form-container').html(HTML).toggle();
$("#calendar-event-form-container").dialog({theme:'a'});
and closes by
$("#calendar-event-form-container").dialog('close');
$("#calendar-event-form-container").toggle();
Next time the dialog is opened it loses its JQM theme and position .
Can anybody see where the code has gone wrong?
Thanks in advance
Sincerely,
Babak
andleer is correct, don't use toggle() with jQM, it is not needed. jQM dialog is created to be used as a separated page. Take a look at this example:
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h3>
First Page
</h3>
Next
</div>
<div data-role="content">
Open dialo
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
<!-- DIALOG BOX -->
<div data-role="page" id="dialog-box" data-theme="b">
<div data-role="header">
<h1>Warning</h1>
</div>
<div data-role="content">
<h3 id="dialog-msg">
Dialog test
</h3>
<a href="#" data-role="button" id="close-button">
Close dialog
</a>
</div>
</div>
Also if possible you should open dialog box like this:
$.mobile.changePage('#dialog-box', {transition: 'pop', role: 'dialog'});
In case you are dynamically changing dialogbox content you must trigger pagecreate on it to restyle it correctly:
$('#dialog-box').trigger("pagecreate");
And here's a full jsFiddle example: http://jsfiddle.net/Gajotres/fXzWj/
Showing and Hiding a form as a dialog with toggle() is non-standard. The jQuery Mobile dialog is designed to show or hide a page <div data-role="page"> container. As such, it is assumed to be outside of the current page and won't be visible until displayed as a dialog.
http://jquerymobile.com/demos/1.2.0/docs/pages/dialog/index.html
"Any page can be presented as a modal dialog by adding the data-rel="dialog" attribute to the page anchor link"