What is jQuery?
jQuery is a fast, concise, JavaScript Library that simplifies how you traverse HTML documents, handle events, perform animations, and add Ajax interactions to your web pages with less of code.
It was released January 2006 at BarCamp New York City by John Resig.
jQuery contains the following features:
- DOM element selections
- DOM traversal and modification, (including support for CSS and basic XPath)
- Events
- CSS manipulation
- Effects and animations
- Ajax
- Extensibility
- Utilities - such as browser version and the each function.
- JavaScript Plugins
jQuery has an interesting concept called chainability.
Example:
$("#something").addClass("smalltext").removeClass("largetext").show();
That one line of code finds elements that have the ID 'something', adds the class 'smalltext' to them, removes the class 'largetext' from them and then shows them if they are hidden.
jQuery also has a number of awesome effects.
Example:
<script>
$("h1").hide();
</script>
jQuery also has a very nice method of detecting events.
Example:
<script>
$("a").click(function(){
alert("You clicked a link!");
});
</script>
That code, placed in your headers, will alert the user whenever a link is clicked.
Identify elements:
jQuery has the same way that CSS identify elements. If you know CSS, It'll be easier for you.
Example:
<html>
<head>
<title>Hi.</title>
<script src="js/jquery.js" type="text/javascript"></script>
</head>
<body>
<h1> Hi there </h1>
<h4>Welcome to my site</h4>
<b> Enjoy yourself </b>
</body>
</html>
Lets try to isolate the 'Hi there' text first.
This is the code you need:
$("h1")
Example (Hiding elements)
<html>
<head>
<title>Hi.</title>
<script src="http://9861.smfforfree4.com/jquery.js" type="text/javascript"></script>
</head>
<body>
<h1> Hi there </h1>
<h4>Welcome to my site</h4>
<b> Enjoy yourself </b>
<script>
$("h1").hide();
</script>
</body>
</html>
That was pretty easy, wasn't it? Now let's hide the bold stuff.
Code:
<Script>
$("b").hide();
</script>
But, what if we had two bold elements and wanted to hide only one?
If this is your document?
Code:
<html>
<head>
<title>Hi.</title>
</head>
<body>
<h1> Hi there </h1>
<h4>Welcome to my site</h4>
<b> Enjoy yourself </b>
<i>I am learning jQuery!</i>
<b>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas metus nisi, euismod vel, mattis eu.</b>
</body>
</html>
How?
Answer: Give it an id.
Lets give it the ID "Lorem".
So this is our document now:
Code:
<html>
<head>
<title>Hi.</title>
</head>
<body>
<h1> Hi there </h1>
<h4>Welcome to my site</h4>
<b> Enjoy yourself </b>
<i>I am learning jQuery!</i>
<b id="lorem">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas metus nisi, euismod vel, mattis eu.</b>
</body>
</html>
Then to hide it, you'd have:
Code:
<script>
$("b#lorem").hide();
</script>
Understood? We affix the id after a '#'.
There are another two methods: using the contains function and using a class.
Lets see how to use a class first.
Let's give the part to hide a class. Classes are usually used by CSS to give element specific properties.
Code:
<html>
<head>
<title>Hi.</title>
</head>
<body>
<h1> Hi there </h1>
<h4>Welcome to my site</h4>
<b> Enjoy yourself </b>
<i>I am learning jQuery!</i>
<b class="lorem">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas metus nisi, euismod vel, mattis eu.</b>
Join the conversation! Your thoughts help the community grow.