jQuery Tutorial
$("div")
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("body").html("welcome to 'jQuery'!"); }); </script> </head> <body></body> </html>Output:
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("div").css("background-color", "blue"); $("div").css("color", "white"); }); </script> </head> <body><div>This is main div content.</div></body> </html>Output:
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("div").css("background-color", "blue"); $("div").css("color", "white"); }); </script> </head> <body> <div>This is first div content.</div> <br/> <div>This is second div content.</div> </body> </html>Output:
$("#id") $('#id')
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("#divFirst").css("text-decoration", "line-through"); $("#divFirst").css("color", "brown"); }); </script> </head> <body> <div id="divFirst">This is first div content.</div> <br/> <div>This is second div content.</div> </body> </html>Output:
$(document).ready(function(){ $("[id=divFirst]").css("text-decoration", "line-through"); $("[id=divFirst]").css("color", "brown"); });
$(document).ready(function(){ // Finds element using starting with 'divF' string $("[id^=divF]").css("text-decoration", "line-through"); // Finds element using ending with 'First' string $("[id$=First]").css("color", "brown"); });
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("#first").css("text-decoration", "line-through"); $("#first").css("color", "brown"); }); </script> </head> <body> <div id="first">This is first div content.</div> <br/> <div>This is second div content.</div> <p id="first">This is paragraph content.</p> </body> </html>Output:
$(document).ready(function(){ // applies style to p element with id as 'first' $("p[id=first]").css("text-decoration", "line-through"); $("p[id=first]").css("color", "brown"); });Output:
$(".class-name")
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $(".text-content").css("text-decoration", "line-through"); $(".text-content").css("color", "brown"); }); </script> </head> <body> <div>This is first div content.</div> <br/> <div>This is second div content.</div> <p class="text-content">This is paragraph content.</p> </body> </html>Output:
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("div.text-content").css("text-decoration", "line-through"); $("div.text-content").css("color", "brown"); }); </script> </head> <body> <div class="text-content">This is first div content.</div> <br/> <div>This is second div content.</div> <p class="text-content">This is paragraph content.</p> </body> </html>Output:
$(document).ready(function(){ $("*").css("text-decoration", "line-through"); $("*").css("color", "brown"); });
$(this)Applies css styles to 'p' html element on clicking in HTML document.
$(document).ready(function(){ $("p").click(function(){ $(this).css("text-decoration", "line-through"); $(this).css("color", "brown"); }); });
$("div:first")Selects first matching div element in html document
$(document).ready(function(){ $("div:first").css("text-decoration", "line-through"); $("div:first").css("color", "brown"); });
$("ul li:first")Selects first matching li element of first matching ul element.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("ul li:first").css("text-decoration", "line-through"); $("ul li:first").css("color", "brown"); }); </script> </head> <body> <ul> <li>This is first line in first ul content.</li> <li>This is second line in first ul content.</li> </ul> <ul> <li>This is first line in second ul content.</li> <li>This is second line in second ul content.</li> </ul> </body> </html>Output:
$("ul li:first-child")Selects first matching li element of all ul element in html document.
<html> <head> <title>The jQuery Example</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script type = "text/javascript"> $(document).ready(function(){ $("ul li:first-child").css("text-decoration", "line-through"); $("ul li:first-child").css("color", "brown"); }); </script> </head> <body> <ul> <li>This is first line in first ul content.</li> <li>This is second line in first ul content.</li> </ul> <ul> <li>This is first line in second ul content.</li> <li>This is second line in second ul content.</li> </ul> </body> </html>Output:
$("[href]")
$(":button")
$("a[target='_blank']") $("a[target!='_blank']")
$("tr:even") $("tr:odd")
jQuery Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page