9866550012
jQuery Selectors
1.
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
$("p").click(function(){
$(this).hide();
});
});
2.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<h2>This
is a heading</h2>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<button>Click
me</button>
</body>
</html>
3.
<!DOCTYPE
html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<h2>This
is a heading</h2>
<p>This
is a paragraph.</p>
<p
id="test">This is another paragraph.</p>
<button>Click
me</button>
</body>
</html>
4.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
</script>
</head>
<body>
<h2
class="test">This is a heading</h2>
<p
class="test">This is a paragraph.</p>
<p>This
is another paragraph.</p>
<button>Click
me</button>
</body>
</html>
5.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h2>This
is a heading</h2>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<button>Click
me</button>
</body>
</html>
jQuery Events
6.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If
you click on me, I will disappear.</p>
<p>Click
me away!</p>
<p>Click
me too!</p>
</body>
</html>
7.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If
you click on me, I will disappear.</p>
<p>Click
me away!</p>
<p>Click
me too!</p>
</body>
</html>
8.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").dblclick(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If
you double-click on me, I will disappear.</p>
<p>Click
me away!</p>
<p>Click
me too!</p>
</body>
</html>
9.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mouseenter(function(){
alert("You entered p1!");
});
});
</script>
</head>
<body>
<p
id="p1">Enter this paragraph.</p>
</body>
</html>
10.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
<p
id="p1">This is a paragraph.</p>
</body>
</html>
11.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mouseleave(function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
<p
id="p1">This is a paragraph.</p>
</body>
</html>
12.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").mousedown(function(){
alert("Mouse down over p1!");
});
});
</script>
</head>
<body>
<p
id="p1">This is a paragraph.</p>
</body>
</html>
13.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#p1").hover(function(){
alert("You entered p1!");
},
function(){
alert("Bye! You now leave p1!");
});
});
</script>
</head>
<body>
<p
id="p1">This is a paragraph.</p>
</body>
</html>
14.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).css("background-color","#cccccc");
});
$("input").blur(function(){
$(this).css("background-color","#ffffff");
});
});
</script>
</head>
<body>
Name:
<input type="text" name="fullname"><br>
Email:
<input type="text" name="email">
</body>
</html>
jQuery Hide/Show
15.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If
you click on me, I will disappear.</p>
<p>Click
me away!</p>
<p>Click
me too!</p>
</body>
</html>
16.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<p>If
you click on the "Hide" button, I will disappear.</p>
<button
id="hide">Hide</button>
<button
id="show">Show</button>
</body>
</html>
17.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
<button>Toggle</button>
<p>This
is a paragraph with little content.</p>
<p>This
is another small paragraph.</p>
</body>
</html>
18.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$(".ex .hide").click(function(){
$(this).parents(".ex").hide("slow");
});
});
</script>
<style
type="text/css">
div.ex
{
background-color:#e5eecc;
padding:7px;
border:solid
1px #c3c3c3;
}
</style>
</head>
<body>
<h3>Island
Trading</h3>
<div
class="ex">
<button
class="hide">Hide me</button>
<p>Contact:
Helen Bennett<br>
Garden House
Crowther Way<br>
London</p>
</div>
<h3>Paris
spécialités</h3>
<div
class="ex">
<button
class="hide">Hide me</button>
<p>Contact:
Marie Bertrand<br>
265,
Boulevard Charonne<br>
Paris</p>
</div>
</body>
</html>
jQuery Fade
19.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate
fadeIn() with different parameters.</p>
<button>Click
to fade in boxes</button>
<br><br>
<div
id="div1"
style="width:80px;height:80px;display:none;background-color:red;"></div><br>
<div
id="div2"
style="width:80px;height:80px;display:none;background-color:green;"></div><br>
<div
id="div3"
style="width:80px;height:80px;display:none;background-color:blue;"></div>
</body>
</html>
20.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeOut();
$("#div2").fadeOut("slow");
$("#div3").fadeOut(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate
fadeOut() with different parameters.</p>
<button>Click
to fade out boxes</button>
<br><br>
<div
id="div1"
style="width:80px;height:80px;background-color:red;"></div><br>
<div
id="div2"
style="width:80px;height:80px;background-color:green;"></div><br>
<div
id="div3"
style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>
21.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeToggle();
$("#div2").fadeToggle("slow");
$("#div3").fadeToggle(3000);
});
});
</script>
</head>
<body>
<p>Demonstrate
fadeToggle() with different speed parameters.</p>
<button>Click
to fade in/out boxes</button>
<br><br>
<div
id="div1"
style="width:80px;height:80px;background-color:red;"></div>
<br>
<div
id="div2"
style="width:80px;height:80px;background-color:green;"></div>
<br>
<div
id="div3"
style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>
22.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeTo("slow",0.15);
$("#div2").fadeTo("slow",0.4);
$("#div3").fadeTo("slow",0.7);
});
});
</script>
</head>
<body>
<p>Demonstrate
fadeTo() with different parameters.</p>
<button>Click
to fade boxes</button>
<br><br>
<div
id="div1"
style="width:80px;height:80px;background-color:red;"></div><br>
<div
id="div2"
style="width:80px;height:80px;background-color:green;"></div><br>
<div
id="div3"
style="width:80px;height:80px;background-color:blue;"></div>
</body>
</html>
jQuery Slide
23.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style
type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid
1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<div
id="flip">Click to slide down panel</div>
<div
id="panel">Hello world!</div>
</body>
</html>
24.
<!DOCTYPE
html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideUp("slow");
});
});
</script>
<style
type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid
1px #c3c3c3;
}
#panel
{
padding:50px;
}
</style>
</head>
<body>
<div
id="flip">Click to slide up panel</div>
<div
id="panel">Hello world!</div>
</body>
</html>
25.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
</script>
<style
type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid
1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<div
id="flip">Click to slide the panel down or up</div>
<div
id="panel">Hello world!</div>
</body>
</html>
jQuery Animate
26.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left:'250px'});
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
27.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
28.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
height:'+=150px',
width:'+=150px'
});
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
29.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
height:'toggle'
});
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
30.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({
left:'250px',
opacity:'0.5',
height:'150px',
width:'150px'
});
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
31.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({height:'300px',opacity:'0.4'},"slow");
div.animate({width:'300px',opacity:'0.8'},"slow");
div.animate({height:'100px',opacity:'0.4'},"slow");
div.animate({width:'100px',opacity:'0.8'},"slow");
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:100px;position:absolute;">
</div>
</body>
</html>
32.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var div=$("div");
div.animate({left:'100px'},"slow");
div.animate({fontSize:'3em'},"slow");
});
});
</script>
</head>
<body>
<button>Start
Animation</button>
<p>By
default, all HTML elements have a static position, and cannot be moved. To
manipulate the position, remember to first set the CSS position property of the
element to relative, fixed, or absolute!</p>
<div
style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>
jQuery Stop Animations
33.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown(5000);
});
$("#stop").click(function(){
$("#panel").stop();
});
});
</script>
<style
type="text/css">
#panel,#flip
{
padding:5px;
text-align:center;
background-color:#e5eecc;
border:solid
1px #c3c3c3;
}
#panel
{
padding:50px;
display:none;
}
</style>
</head>
<body>
<button
id="stop">Stop sliding</button>
<div
id="flip">Click to slide down panel</div>
<div
id="panel">Hello world!</div>
</body>
</html>
34.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#start").click(function(){
$("div").animate({left:'100px'},5000);
$("div").animate({fontSize:'3em'},5000);
});
$("#stop").click(function(){
$("div").stop();
});
$("#stop2").click(function(){
$("div").stop(true);
});
$("#stop3").click(function(){
$("div").stop(true,true);
});
});
</script>
</head>
<body>
<button
id="start">Start</button>
<button
id="stop">Stop</button>
<button
id="stop2">Stop all</button>
<button
id="stop3">Stop but finish</button>
<p>The
"Start" button starts the animation.</p>
<p>The
"Stop" button stops the current active animation, but allows the
queued animations to be performed afterwards.</p>
<p>The
"Stop all" button stops the current active animation and clears the
animation
queue; so all animations on the element is stopped.</p>
<p>The
"Stop but finish" rushes through the current active animation, then
it stops.</p>
<div
style="background:#98bf21;height:100px;width:200px;position:absolute;">HELLO</div>
</body>
</html>
jQuery HTML Get Content and Attributes
35.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
alert("Text: " +
$("#test").text());
});
$("#btn2").click(function(){
alert("HTML: " +
$("#test").html());
});
});
</script>
</head>
<body>
<p
id="test">This is some <b>bold</b> text in a
paragraph.</p>
<button
id="btn1">Show Text</button>
<button
id="btn2">Show HTML</button>
</body>
</html>
36.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Value: " +
$("#test").val());
});
});
</script>
</head>
<body>
<p>Name:
<input type="text" id="test" value="Mickey
Mouse"></p>
<button>Show
Value</button>
</body>
</html>
37.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert($("#w3s").attr("href"));
});
});
</script>
</head>
<body>
<p><a
href="http://www.w3schools.com"
id="w3s">W3Schools.com</a></p>
<button>Show
href Value</button>
</body>
</html>
jQuery HTML Set Content and Attributes
38.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text("Hello
world!");
});
$("#btn2").click(function(){
$("#test2").html("<b>Hello
world!</b>");
});
$("#btn3").click(function(){
$("#test3").val("Dolly
Duck");
});
});
</script>
</head>
<body>
<p
id="test1">This is a paragraph.</p>
<p
id="test2">This is another paragraph.</p>
<p>Input
field: <input type="text" id="test3" value="Mickey
Mouse"></p>
<button
id="btn1">Set Text</button>
<button
id="btn2">Set HTML</button>
<button
id="btn3">Set Value</button>
</body>
</html>
39.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("#test1").text(function(i,origText){
return "Old text: " + origText
+ " New text: Hello world! (index: " + i + ")";
});
});
$("#btn2").click(function(){
$("#test2").html(function(i,origText){
return "Old html: " + origText
+ " New html: Hello <b>world!</b> (index: " + i +
")";
});
});
});
</script>
</head>
<body>
<p
id="test1">This is a <b>bold</b> paragraph.</p>
<p
id="test2">This is another <b>bold</b>
paragraph.</p>
<button
id="btn1">Show Old/New Text</button>
<button
id="btn2">Show Old/New HTML</button>
</body>
</html>
40.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href","http://www.w3schools.com/jquery");
});
});
</script>
</head>
<body>
<p><a
href="http://www.w3schools.com"
id="w3s">W3Schools.com</a></p>
<button>Change
href Value</button>
<p>Mouse
over the link (or click on it) to see that the value of the href attribute has
changed.</p>
</body>
</html>
41.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href","http://www.w3schools.com/jquery");
});
});
</script>
</head>
<body>
<p><a
href="http://www.w3schools.com"
id="w3s">W3Schools.com</a></p>
<button>Change
href Value</button>
<p>Mouse
over the link (or click on it) to see that the value of the href attribute has
changed.</p>
</body>
</html>
42.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#w3s").attr("href",
function(i,origValue){
return origValue + "/jquery";
});
});
});
</script>
</head>
<body>
<p><a
href="http://www.w3schools.com"
id="w3s">W3Schools.com</a></p>
<button>Change
href Value</button>
<p>Mouse
over the link (or click on it) to see that the value of the href attribute has
changed.</p>
</body>
</html>
jQuery HTML Add Elements/Content
43.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").append(" <b>Appended
text</b>.");
});
$("#btn2").click(function(){
$("ol").append("<li>Appended
item</li>");
});
});
</script>
</head>
<body>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<ol>
<li>List
item 1</li>
<li>List
item 2</li>
<li>List
item 3</li>
</ol>
<button
id="btn1">Append text</button>
<button
id="btn2">Append list items</button>
</body>
</html>
44.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").prepend("<b>Prepended text</b>.
");
});
$("#btn2").click(function(){
$("ol").prepend("<li>Prepended
item</li>");
});
});
</script>
</head>
<body>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<ol>
<li>List
item 1</li>
<li>List
item 2</li>
<li>List
item 3</li>
</ol>
<button
id="btn1">Prepend text</button>
<button
id="btn2">Prepend list item</button>
</body>
</html>
45.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
function
appendText()
{
var
txt1="<p>Text.</p>"; // Create text with HTML
var
txt2=$("<p></p>").text("Text."); // Create text with jQuery
var txt3=document.createElement("p");
txt3.innerHTML="Text."; // Create text with DOM
$("body").append(txt1,txt2,txt3); // Append new elements
}
</script>
</head>
<body>
<p>This
is a paragraph.</p>
<button
onclick="appendText()">Append text</button>
</body>
</html>
jQuery Get and Set CSS Classes
46.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").addClass("blue");
$("div").addClass("important");
});
});
</script>
<style
type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1>Heading
1</h1>
<h2>Heading
2</h2>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<div>This
is some important text!</div>
<br>
<button>Add
classes to elements</button>
</body>
</html>
47.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").addClass("important blue");
});
});
</script>
<style
type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<div
id="div1">This is some text.</div>
<div
id="div2">This is some text.</div>
<br>
<button>Add
classes to first div element</button>
</body>
</html>
48.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").removeClass("blue");
});
});
</script>
<style
type="text/css">
.important
{
font-weight:bold;
font-size:xx-large;
}
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1
class="blue">Heading 1</h1>
<h2
class="blue">Heading 2</h2>
<p
class="blue">This is a paragraph.</p>
<p>This
is another paragraph.</p>
<br>
<button>Remove
class from elements</button>
</body>
</html>
49.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("h1,h2,p").toggleClass("blue");
});
});
</script>
<style
type="text/css">
.blue
{
color:blue;
}
</style>
</head>
<body>
<h1>Heading
1</h1>
<h2>Heading
2</h2>
<p>This
is a paragraph.</p>
<p>This
is another paragraph.</p>
<button>Toggle
class</button>
</body>
</html>
jQuery css() Method
50.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("Background color = " +
$("p").css("background-color"));
});
});
</script>
</head>
<body>
<h2>This
is a heading</h2>
<p
style="background-color:#ff0000">This is a paragraph.</p>
<p
style="background-color:#00ff00">This is a paragraph.</p>
<p
style="background-color:#0000ff">This is a paragraph.</p>
<button>Return
background-color of p</button>
</body>
</html>
51.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
var txt="";
txt+="Width of div: " +
$("#div1").width() + "</br>";
txt+="Height of div: " +
$("#div1").height() + "</br>";
txt+="Inner width of div: " +
$("#div1").innerWidth() + "</br>";
txt+="Inner height of div: " +
$("#div1").innerHeight();
$("#div1").html(txt);
});
});
</script>
</head>
<body>
<div
id="div1"
style="height:100px;width:300px;padding:10px;margin:3px;border:1px solid
blue;background-color:lightblue;"></div>
<br>
<button>Display
dimensions of div</button>
<p>innerWidth()
- returns the width of an element (includes padding).</p>
<p>innerHeight()
- returns the height of an element (includes padding).</p>
</body>
</html>
52.
<!DOCTYPE
html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").css("background-color","yellow");
});
});
</script>
</head>
<body>
<h2>This
is a heading</h2>
<p
style="background-color:#ff0000">This is a paragraph.</p>
<p
style="background-color:#00ff00">This is a paragraph.</p>
<p
style="background-color:#0000ff">This is a paragraph.</p>
<p>This
is a paragraph.</p>
<button>Set
background-color of p</button>
</body>
</html>