Ever since the Internet came into existence, web developers have been immensely bothered about the proper placement of text and images on their web pages. Like any other remarkable tool, jQuery has made the front-end coding task absolutely simple and hassle-free. Whether you’re inclined on building a robust website or a complex web application, jQuery has all the components that can allow you to meet your purpose.
Well, its quite common in web applications that clicking on a specific image will change the text that’s been displayed on it previously. Today, through this blog I’ll be covering a few ways in which you can make the most of jQuery, JS and CSS for developing a content-switching component.
Table of Contents
JQuery – Using It For Building A Content-Switching Component:
Here’s the jQuery function that’s used for toggling the display of a chosen content block:
$(function() { $('.jqueryOptions').hide(); $('#choose').change(function() { $('.jqueryOptions').slideUp(); $('.jqueryOptions').removeClass('current-opt'); $("." + $(this).val()).slideDown(); $("." + $(this).val()).addClass('current-opt'); }); });
The above function will look for all the content blocks with a class of ‘jqueryOptions‘ and hide them. In other words, when a user changes the selected option of the select input, the function would close the open content blocks with the help of jQuery’s .slideUp()
method. After this, the same function would open the selected content blocks using the slideDown()
method.
Vanilla JavaScript – What Its All About?
Javascript developers who are looking forward to build a small-to-medium sized library can opt for Vanilla Javascript. As a fantastic resource, Vanilla Javascript matches up with the jQuery code snippets. Although the difference between selecting elements in jQuery and in Vanilla Javascript isn’t large, if you’re heavily reliant on varied element selectors then vanilla Javascript can turn to be your right choice.
A Brief Overview On Creating A Vanilla JavaScript Version:
Now, here are the steps involved in creation of a Vanilla Javascript version:
Step 1- Define A Few VariablesL:
As the very first step, you need to define some vital variables as explained below:
var selectInput = document.getElementById('choose'), panels = document.querySelectorAll('.options'), currentSelect, i;
In the above declaration, all the variables can access the input select(selectInput)
, the placeholder for current selection, different content panels(panels) and the iterator.
Step 2:) Now, Write Some Functions For Managing The Above Variables:
Prior to writing the function for the i; it is crucial for you to ensure that any content available within the view is being hidden in case a new option is being selected. Hence, you’ll have to create the variable’s own function as explained below:
function clearShow() { for ( i = 0; i < panels.length; i++ ) { panels[i].classList.remove('show'); } }
In the above function, the clearShow() function is taking the panels variable and simultaneously removing the class of “show” from each one of them. It is the show class which makes the content visible on the particular web page.
Now, it’s time for using a function that can remove the ‘show‘ class from every content block. For this, the function will be declared as below:
function addShow(showThis) { var el = document.getElementsByClassName(showThis); for ( i = 0; i < el.length; i++ ) { el[i].classList.add('show'); } }
In the above function, addShow()
function will receive an argument called showThis and add the ‘show‘ class to the panel node that contains a class which matches the current value of the select input.
Step 3:) Use A Suitable Function For Passing The showThis
Value To addShow()
:
Here’s the function that would be used for passing the showThis
value to addShow()
:
function vUpdate() { currentSelect = selectInput.value; clearShow(); addShow(currentSelect); } selectInput.addEventListener('change', vUpdate);
In the above function, vUpdate will be executed each time the select input is being updated.
Is CSS Sufficient For Building The Content-Switching Component?
While the jQuery and Vanilla Javascript solutions can be easily based on the same markup, the CSS-only web solution needs to be modified for building the content-switching component. One of the biggest changes introduced in CSS is that the ‘select’ has been replaced as a whole. Thus, utilizing different values for the web page toggle won’t be supported by CSS alone.
Wrapping Up:
So, that was an elaboration on how jQuery, JS and CSS can be utilized for building content-switching component. Hope the details covered above would guide you throughout the journey of building a fine-quality content-switching panel that can add a unique charm to your website.
that was valuable information about queries… and it was easy to understand
thanks for sharing it …
Welcome here and thanks for liking our article. Stay with us to get more like this…
Wow! I am so thankful I found this blog, the tips are easy and clear. I love this awesome post. Thanks for sharing.
Welcome here and thanks for liking our article. Stay with us to get more like this…
Excellent Work in article it’s a very helpful for me thank to share this info.
Welcome here and thanks for liking our article. Stay with us to get more like this…