Twitter is familiar for all of us now…Listing twitter feeds using Jquery will be fun as well…Let’s create a Jquery – Twitter widget that can be used in our website. I have only started off with learning Jquery. So i guess this might be easy for beginners of Jquery…
Our Jquery-Twitter widget will have 3 tabs. The first tab will show the public twitter timeline (means the 20 most recent statuses from non-protected users). The next tab shows the latest 5 tweets of the user and the final tab shows the Twitter profile info of the user.
The first step is to create the Jquery widget. Later we will call the Twitter feeds using inbuilt ajax functions of Jquery.
Step 1 : Creating the Jquery Widget
Creating a Jquery Widget have been wonderfully explained in Webitect. You can refer that tutorial here. Below is my version.
Create an HTML page with the following code:-
Here we include the jquery library inside the script tag. We then create the skeletal structure of our widget. The three divs corresponds to three tabs. We give the title for each tab in the <h5> tag.
So the basic structure is ready. Now let’s use Jquery to create a tabbed appearence.
The above code runs as soon as the page runs. Here we are creating a main div (with class name widget-wrapper) that holds the Widget. Inside this main div we create a unordered list with class tab-wrapper that holds the tab name and then another div with class name new-widget that holds the content inside each tab. We create an html string for these and assigns to a variable newWidget. Next step is to hide all elements with class name widget . Now, we’ll use the first widget as a reference point to insert our tabbed container, using the jQuery before() function.
Now we will loop thru each unordered lists inside a div with class widget and then append a list with class “tab” and the text inside the <li> as the id of each <ul>. Please note that the id of each <ul> will be shown as the tab header.
So our Jquery widget is created in tabbed format with no content in it. Let’s fill each tabs with twitter content while clicking the tab header. We use separate twitter API methods to fill the content for each tab. The code is as below:-
The getJSON function calls the required twitter method and then generates the html content inside its success function.
Also make sure that on page load one tab always needs to be selected. For that just before the closing of the $(document).ready function give the following code:-
So we have created a simple Jquery-Twitter widget. I hope its not very confusing for u….If you have any queries please feel free to get back in touch.
You can see a running example here.