jQuery Basic
jQuery Selectors
- jQuery Selectors
- jQuery *
- jQuery :animated
- jQuery [name|=”value”]
- jQuery [name*=”value”]
- jQuery [name~=”value”]
- jQuery [name$=”value”]
- jQuery [name=”value”]
- jQuery [name!=”value”]
- jQuery [name^=”value”]
- jQuery :button
- jQuery :checkbox
- jQuery :checked
- jQuery Child Selector
- jQuery .class
- jQuery :contains()
- jQuery Descendant Selector
- jQuery :disabled
- jQuery Element
- jQuery :empty
- jQuery :enabled
- jQuery :eq()
- jQuery :even
- jQuery :file
- jQuery :first-child
- jQuery :first-of-type
- jQuery :first
- jQuery :focus
- jQuery :gt()
- jQuery Has Attribute
- jQuery :has()
- jQuery :header
- jQuery :hidden
- jQuery #id
- jQuery :image
- jQuery :input
- jQuery :lang()
- jQuery :last-child
- jQuery :last-of-type
- jQuery :last
- jQuery :lt()
- jQuery [name=”value”][name2=”value2″]
- jQuery (“selector1, selector2, selectorN”)
- jQuery (“prev + next”)
- jQuery (“prev ~ siblings”)
- jQuery :not()
- jQuery :nth-child()
- jQuery :nth-last-child()
- jQuery :nth-last-of-type()
- jQuery :nth-of-type()
- jQuery :odd
- jQuery :only-child
- jQuery :only-of-type
- jQuery :parent
- jQuery :password
- jQuery :radio
- jQuery :reset
- jQuery :root
- jQuery :selected
- jQuery :submit
- jQuery :target
- jQuery :text
- jQuery :visible
jQuery :target Selector
Photo Credit to CodeToFun
🙋 Introduction
In web development, creating dynamic and interactive user experiences is essential. jQuery, a powerful JavaScript library, provides a range of selectors to simplify DOM manipulation and event handling. Among these selectors, the :target
selector stands out for its ability to target elements based on their hash fragment identifier in the URL.
This guide explores the jQuery :target
selector, its syntax, and practical examples to help you leverage its capabilities effectively.
🧠 Understanding :target Selector
The :target
selector is used to select the target element associated with the current URL's hash fragment identifier (the portion of a URL that follows the # symbol). This selector is particularly useful for creating single-page applications (SPAs) and implementing navigation within a webpage without reloading the entire page.
💡 Syntax
The syntax for the :target
selector is straightforward:
$(":target")
📝 Example
Styling the Target Element:
Suppose you have a navigation menu with links that scroll to different sections of a webpage using hash fragment identifiers. You can use the
:target
selector to style the targeted section when the link is clicked. For instance:index.htmlCopied<nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> </ul> </nav> <section id="section1">Content of Section 1</section> <section id="section2">Content of Section 2</section>
example.jsCopiedsection:target { background-color: lightblue; }
This CSS rule will change the background color of the targeted section when its corresponding link is clicked.
Implementing Smooth Scroll Behavior:
You can enhance the user experience by adding smooth scrolling to the targeted sections when a navigation link is clicked. jQuery can help achieve this effect:
index.htmlCopied<nav> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> </ul> </nav> <section id="section1">Content of Section 1</section> <section id="section2">Content of Section 2</section>
example.jsCopied$("a").on('click', function(event) { if (this.hash !== "") { event.preventDefault(); var hash = this.hash; $('html, body').animate({ scrollTop: $(hash).offset().top }, 800, function(){ window.location.hash = hash; }); } });
This script ensures a smooth scroll animation to the target section when a navigation link is clicked.
🎉 Conclusion
The jQuery :target
selector provides a convenient way to manipulate elements based on the hash fragment identifier in the URL. Whether you want to style the targeted element, implement smooth scrolling, or create dynamic navigation within a single-page application, this selector offers versatile functionality.
By understanding its usage and incorporating it into your projects, you can enhance the user experience and create more engaging web applications.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (jQuery :target Selector), please comment here. I will help you immediately.