Thursday, April 23, 2009

Using jQuery - Attributes 1

As I said before , selectors makes jquery powerful . In this post I want to write about attribute selector.
Its very easy ... here is a sample :
$("a[href='http://www.google.com']").
In this example $("a") select each link on the page , and [href='http://www.google.com'] restrict the selector to 'a' tags with href attribute equals to 'http://www.google.com' .

example :
I have a list of some files like below


If you want to show this list with appropriate icon for each type of file , you must add an img tag before each link . jquery has .before() and .after() method for insert content after and before selected tag(s) by selector.
Ok . Now I want to select each link in the page with href attribute ends with '.pdf' and insert an img tag befor using .before() method.



dont worry about $(document).ready . Its assign a function to onload event of document .




and now lets finish it ...



HTML result :


No comments:

Post a Comment