Hi Vikram, you are right, java script is not right way of dealing it. Test automation script should exercise application as real user does and real user would not execute java script to exercise any element.
About the css locator, I assume that you have seen first and second video tutorial. But the best source to learn css locator is -
http://www.w3.org/TR/CSS2/selector.html
~ seleniumtests.com