Monday, July 9, 2012

Fast Method for Learning jQuery using JavaScript Console

I already know jQuery for some years. But I just using jQuery excessively in my current project. One of requirement on this project is to minimize request to server either ajax or non ajax. So all processing is done in client and request to server is done only if very necessary. From this project I found one useful method in learning jQuery.

jQuery itself is JavaScript library which give developer facility to select and or modify part of html page using a kind of css selector. So learning jQuery is learning to use it selector. Previously my method in learning jQuery is by writing one html file and write my jQuery code inside one script tag and inside jQuery ready method as follow:

The jQuery ready method is very useful. Since jQuery aim is to process document structure of html then we have to make sure all html document object already loaded before jQuery script processing. We have to remember JavaScript which jQuery run for is interpreter language, it is processed from top to down. jQuery ready method fulfil that requirement.

Putting my learning code inside that block is not wrong in learning process. After writing our script we can refresh the page and see the result. Later we can edit our jQuery script inside that html file, save and refresh again. But there are other way to learn faster. We can learn more effective by using Google Chrome javascript console or Firebug in Firefox (other browser also have similar tools we can just use that as well). Buy using this tool we only need to load one html file for learning and playing the jQuery script inside the console. We can try various selector and manipulate document object model (dom). We don't need to go through type, save, refresh cycle anymore. We only need to edit html if we want to add more html structure for experiment. Otherwise we only need to interact with JavaScript console.

Let's go to practice by copy paste following html script into one file:

<title>Learn jQuery</title>
<script src=""></script>
 // jQuery script here
<h1>Welcome to My Homepage</h1>
<p class="intro">This is Intro</p>
<p>This is paragraph</p>
<p>Another Paragraph</p>
Some list:
<ul id="choose">
<div>Inside some div</div>

After saving file just open it using google chrome or firefox with firebug installed. This post will consider using google chrome only. If you are using other browser please adjust accordingly. Following is rendered page from above html script.

To open JavaScript console in Google chrome, we can go to wrench menu --> Tools --> JavaScript Console. Or we can just hit Ctrl + Shift + J. It will open following console at bottom browser:

From that console we can start play with jQuery. Just type "jQuery" to make sure jQuery setup properly. If showing like this than it already setup properly. 

By the way this post assume that you have internet connection because jQuery library referenced in html is loaded from CDN on internet. If you don't have internet connection make sure you download jQuery js file and referenced accordingly in src script attribute correctly.

Let start to use id selector type  jQuery('#choose')  in console. It will show the result below the script we type.
If jQuery cannot find the tree it will show empty bracket [] since selector found empty element.

Let's try to select element directly using  jQuery('li') . This will select all three elements of li tags.

Next let's play little bit with filter and start to play with manipulation. Type this inside the console jQuery('li:first').css('color','red'). This script will select first li in document and set it css property color to red. Final result can be seen below:

Ok that's all for now. We can see more complete capability of jQuery by reading its API at . Using jQuery is the matter of getting used to know its selector. By using javascript console we can learn faster. 

Living in Singapore

Today I just saw my desk calendar. Little bit surprised when I realized it is already July 2012. It's mean more than 7 months I have work in Singapore. Of course I have been getting used with daily life here. When little bit sharing some history, since really first time I really want to work abroad. Even if I am not mistake, since I was in university. The chance finally came by itself when fortunately there was one project from one of IT consultant in Singapore which need human resources which very match with my profile. After several recruitment and selection processing I arrived in Singapore on end of November 2011.

Going overseas will force us to getting adapted to different environment and situation. There will be always good side and challenging side for everything. I will try to share those things in this post.

Good Things

Let's start with good thing. Singapore is very advance country. We can enjoy many great public facility. One of them is transportation. I think Singapore has the most advance public transportation system. Singapore has already long running MRT (Mass Rapid transportation) system that connect almost between all edge of Singapore and still growing. In following years will be added another new MRT line. Beside MRT public bus will connect MRT line to resident area or other area. Either bus and MRT are very clean same as well with MRT station and bus terminal.

Most of those transportation support cashless payment. The most widely used is EZlink card. By using this card we can enjoy integrated payment mode even if use combination of bus and MRT while we travel. We just need to tap the card to available reader on bus or at MRT station entrance and tap again when we alighting from bus or go out from MRT station. When we transfer mode of transportation for example from bus to MRT or from bus to bus, the fee will be calculated accordingly which a lot cheaper compare to cash payment. Of course cash payment still available some time. But for daily commuter cash payment will be lest economic and  slow.

Other great public facility in Singapore are sport facility. In almost every area there will sport center. In that facility usually available badminton court, basketball court, gym and swimming pool. All of those facility can be accessed by all people with affordable fee. Regarding swimming pool, Singapore is a country which has biggest density of swimming pool per area.

Move to other public facility is library. As advance country Singapore government fully aware to the important of education for society. Like sport facility, in every area there always a public library that can be accessed for 7 days per week from morning until 9 PM. The library have very update collection. For citizen membership is very cheap. For foreigner even though the fee is not as cheap as citizen but still reasonable. Library also placed in easy to access location like mall and near MRT station which can encourage people to visit often.

Next is park. Also similar with sport facility and library, Singapore has so many open park. Even though Singapore is small country but authorities here always try to maintain nature environment like park or city forest. In every of that open park there will be jogging track, lake, playing ground for children or even barbecue pit for gathering of people.

Beside facility Singapore also has minimum criminal act. We usually don't get too worried even though for example going home late at night. Because maybe since unemployment rate is minimum, automatically criminal act also very low.

Challenging Things

Let's next elaborate some of challenge when we have to come to Singapore. First, for small country Singapore is very diverse. As one of the proof, when we are in public transport, try to listen surrounding for one time. We can here one person speak mandarin on the phone, one couple will discuss in English, other group of people speaking tamil or maybe hindi, one other person also talk in the phone using Tagalog, three of people at the corner speaking Malay or maybe Indonesian language (Malay and Indonesian language even though heard similar but actually it different) and at different car other group of people will speak with some weird European language. It is mean we have to flexible with this diverse environment and always try to respect each other. Especially each people from different country will have their own culture and habit.

Most important challenge in Singapore is life expenses. Singapore is one of the most expensive country in the world. For someone who has first time job offer in here please don't be happy too soon when getting salary offer that seems so high. Everything in Singapore is quite expensive. The biggest expensive for living in Singapore is accommodation. Renting whole unit government subsidize apartment will be cost minimum 2000 SGD. The price will be higher 2 or even 3 to 4 times if renting unit in private apartment or condo. Cheaper solution for person that still single is to rent room only and sharing common area like dinning room, living room, kitchen and maybe bathroom with other tenant or owner.

Other expense is for daily consumption. Buy assuming one time cheap meal in food court for 5SGD then perday we will spend about 15 SGD. And per month will be 450SGD. Of course this is very minimum assumption. Other expense will be transportation. Per journey we can assume cost about 1.5 SGD so perday will be about 3 SGD. We haven't calculate for telecommunication and other emergency expense (maybe later I will write post dedicated for this topic). At least we can see quite clear that living expense is very high.

For the people who first time come to Singapore will be very enjoy with this country. But for the person who stay more than 6 months usually we can get bored easily. In this country we can go from one side to other side of country for less than one hour. It mean we can run out place to see very soon. My wife one time make a joke. She said it difficult to distinguish area in Singapore. Every area almost exactly same. Every intersection is very similar. I have to admit her statement is quite true. But don't get scary to die soon because boring. Actually if we discover more, there will be always good place to see in this nice country. We can just tracking to city forest, window shopping to so many mall in this country or just get on to the double decker and go randomly to any bus direction. Or perhaps if die die (this is Singlish) boring so much we just can visit neighbor county like Malaysia or Indonesia.

However even though challenging to live here, I have to admit again that this country is good and I love staying here.