Creating tabbed interfaces using jQuery and CSS

I'm currently working on creating a feature similar to the bottom bar in GMail or Facebook's chat window. Just to clarify, I am not attempting to develop a chat service or anything of that nature.

In Facebook's chat function, when you click on a friend, a rectangular block appears at the bottom of the visible page with your friend's name and chat history.

I was curious about how to generate tabs at the bottom of the page based on a list of items, such that clicking on each item would display its corresponding information in a tab?

Would it be necessary to pre-generate tabs for each item and then set them to display:none, only showing them when clicked, as shown in the pseudo-code below?

$(##tab attribute##).click(function {
        ##selected tab##.show()
     }
 )

Answer №1

If I understand correctly... There's no need to pre-create tabs as you can generate them dynamically. Check out this demo for a simple example.

UPDATE

To provide more details, following AlienWebguy's advice:

Start by making a list of links and a container for the tabs. Each link should have a unique id:

html:

<!-- Links to different sections -->
<ul id="chat-links">
    <li><a id="foo" href="#">foo</a></li>
    <li><a id="bar" href="#">bar</a></li>
    <li><a id="baz" href="#">baz</a></li>
</ul>

<!-- Container for the tabs -->
<div id="chat-stage"></div>

Next, create a click event for each link. When clicked, it will display a custom message in a 'chat-window' div:

jQuery:

// Click event
$('#chat-links a').click(function() {
    // Custom message
    var str = "Hi, " + this.id + ", what's up?";
    // Append a new div to the tab container
    $('#chat-stage').append('<div class="chat-window">' + str + '</div>');
});

Then style the elements:

css:

#chat-stage
{
    position: absolute;
    bottom: 0px;  
}

.chat-window
{
    border: 1px solid #ccc;
    height: 100px;
    float: left;
    margin-right: 8px;
    padding: 8px;
    width: 100px;  
}

This is a basic implementation that allows multiple tabs to be opened by clicking on the same link. You can adjust the click event to handle this situation, such as generating a unique id for each tab or implementing a conditional statement. Additionally, you may want to close other tabs when opening a new one. In this case, you can add $('#chat-stage').empty(); at the beginning of your click event.

Hope this explanation helps :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to replicate an ajax GET request with data parameters in Postman?

Is it possible to simulate an API call using Postman if I only know how to do it in jQuery's ajax? Here is the ajax code for reference: ajax: { url : API_URL, data: function (data) { data.authenticityToken = TOKEN; } } Can anyone ...

Incorrect form element style is only visible when contained within an unstyled HTML Details tag

A demonstration form control extracted from the Bulma CSS framework documentation performs as anticipated with Bulma 0.7.2 (most up-to-date version at the time of writing). However, when this form is contained within a standard html <details> tag, t ...

What are alternative methods to prevent a div from expanding in width from the right side other than using position absolute?

Looking to create a div element that only expands from the left side as content is added? <div id="sent_message" style='background-color: #2b89cc; color: white; font-family: Arial,Helvetica, sans-serif; margin-top: 10px; display: ...

Automatically retrieve elements via AJAX once the call is made

Is there a more efficient way to select or manipulate elements retrieved from an AJAX call without having to use the .on/.live method in jQuery each time? ...

Polymer elements fail to adapt to varying screen sizes

I am currently working on creating a responsive website using polymer, but I have encountered an issue where certain elements (such as core-toolbar and paper-fab) are not scaling properly on smaller, denser screens like smartphones. After browsing through ...

Align text vertically within a link box by overriding inherited CSS styles

Fiddle: http://jsfiddle.net/jgallaway81/ax9wh/ <a href="lcars.jfx.php" class="leftbuttons buttonlinks antibutton"> LCARS Locomotive O.S. </a> My issue pertains to the alignment of text within a graphic. I am utilizing this particular button ...

Height Issue with Nested Columns in Bootstrap 5

My nested columns are giving me trouble with their height. I want them to have equal heights on both big and small screens. Currently, the sizes look perfect on the largest and smallest viewports, but not on medium-sized ones. Check out examples on codepen ...

Several problems with CSS regarding DIV height set to 100% and the content inside

I'm facing a bit of a puzzle here. I have a div that spans the full height of the browser, with a background set to 100% size and image content contained in two inline divs that are vertically aligned in the middle. In one of these divs, the image is ...

jQuery Selectors with Variable

Having trouble incorporating variables in selectors using jQuery. It's a small issue, but quite frustrating! Check out this jsFiddle for an example: The code snippet below is not functioning as expected: var folder_id = "folder"; $("#selects select ...

What could be causing my navbar to not be responsive on mobile devices?

I'm in need of assistance. When viewing my website in mobile mode, the collapse hamburger menu is hiding too far to the right. This issue is making the site look unresponsive, and I've been unable to find a solution. It seems like the problem sta ...

The functionality of the angularjs class seems to be malfunctioning

I'm a new learner of angularjs and I've created a simple page below. I have a style called "item" that I'm trying to apply to a div, but it's not working. However, if I use inline style, then it works fine. Can someone please help me f ...

What happens to the page layout when a user adjusts the window size?

Is there a way to ensure that a div remains at 40% of the full screen width, regardless of window size changes? I am looking for a JavaScript or JQuery code that can lock the setting of the div to always be 40% of the full screen size. <div style="wi ...

Customizable initiation time for the JQuery Countdown timer

I am in need of a customizable timer feature for my website that allows users to input their desired countdown duration in minutes and seconds. Additionally, having a start/stop function would be ideal. I have looked around for a suitable solution, but mo ...

Getting the Value of my Latest Selection in Bootstrap Multiselect

I have a question regarding the following code snippet: <select name="department" id="department-list" class="form-control" onChange="getTeams3(this.value);" multiple> <option value="MONGOOSEAHOY" selected>All departments</option> &l ...

The functionality of Dropdown varies when using the .find().first() and .next() methods

My goal is to create a dropdown that opens when a button (image) is clicked and closes when anything else on the page is clicked. I want the dropdown to slide down smoothly using slideToggle(). However, I've encountered an issue. After attempting to ...

ignore any anchors beyond the second index

I am working with a django-qcm that has dynamic sections triggered by anchors and utilizes the scroll snap type property. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv ...

How can I effectively vertically position a button in Bootstrap 5 without compromising its functionality?

I'm attempting to reposition the sign-up buttons for Google, Apple, and email below where it says "join," but they stubbornly refuse to budge even when I try to force them with CSS. https://i.sstatic.net/74LQk.jpgCSS .homepage_topRight_Buttons{ ...

Comparing Strict CSS with Hacky CSS - Which is the Better Choice?

When working with CSS, it becomes evident fairly quickly that certain styles are not universally compatible across different browsers. For instance, achieving a semi-transparent PNG required a convoluted solution for Internet Explorer like: filter: pro ...

What is the most efficient method for retrieving an element using a data attribute with an object (JSON) value?

Imagine having the following HTML element: <div class='element' data-info='{"id":789, "value":"example"}'></div> By running this JavaScript code, you can access the object stored in the data attribute. console.log($(&apos ...

The sidebar remains fixed in height and does not expand vertically

Need Help: My sidebar won't expand in height when I update content. Is there a way to make it adjust using just HTML and CSS? html code: <div id="main"> <section> More text goes here... </section> <div id="sideb ...