Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI recently came through a website and checked its functionality. I visited their main javascript file and came through this code.
var featureIconHolder = $(".feature-icon-holder", "#features-links-holder");
featureIconHolder.on("click",function(){
featureIconHolder.removeClass("opened");
$(this).addClass("opened");
$(".show-details","#features-holder").removeClass("show-details");
$(".feature-d"+$(this).data("id"), "#features-holder").addClass("show-details");
});
I am unable to understand how he stores two things in one variable at a time. Also what is $(this).data("id") . Will please someone explain me this code in detail. Thanks in advance.
Closed. This question is off-topic. It is not currently accepting answers.Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI recently came through a website and checked its functionality. I visited their main javascript file and came through this code.
var featureIconHolder = $(".feature-icon-holder", "#features-links-holder");
featureIconHolder.on("click",function(){
featureIconHolder.removeClass("opened");
$(this).addClass("opened");
$(".show-details","#features-holder").removeClass("show-details");
$(".feature-d"+$(this).data("id"), "#features-holder").addClass("show-details");
});
I am unable to understand how he stores two things in one variable at a time. Also what is $(this).data("id") . Will please someone explain me this code in detail. Thanks in advance.
Your specific questions:
var featureIconHolder = $(".feature-icon-holder", "#features-links-holder");
Here's the documentation link for this: http://api.jquery/jQuery/#jQuery1
The two arguments here are selector and context. What this does is find all elements of class feature-icon-holder
that are descendents of the element with ID features-links-holder
. This constructs an array-like object that contains 0+ elements depending on how many matched.
what is
$(this).data("id")
Inside the click handler, this
is the element that has been clicked. So $(this)
is a jQuery object for that element, and .data
reads or stores values against an element. It's then used to construct another selector by concatenating strings.
This changes the displayed 'feature' based on the icon clicked. What it does:
feature-icon-holder
inside the element with ID feature-links-holder
.feature-holder
that currently has the class; again there's probably only one of these, the currently displayed featurefeature-holder
that has a class name matching the ID stored with the icon clicked. This will presumably show the feature elsewhere on the page.
$(".feature-icon-holder", "#features-links-holder")
? – Rup Commented Mar 15, 2019 at 15:23