Using javascript you can do, but it is recommended to use Jquery to reduce the code.
For instance using Javascript :
function findMatches(){
var matches = [];
var searchEles = document.getElementsByClassName(“parent”)[0].children;
for(var i = 0; i < searchEles.length; i++) {
if(searchEles[i].tagName == ‘P’) {
if(searchEles[i].getAttribute(‘class’) == ‘child’) {
matches.push(searchEles[i]);
}
}
}
}
In this case, all the matches corresponding to the parent element will be returned.
When in Jquery, your function will be like this :
function findMatches(){
return $(‘.parent’).find(‘.child’);
}
I would personally recommend to use jquery for these operations because of lesser code and compressed javascript output on browser.
Answer ( 1 )
Please briefly explain why you feel this answer should be reported .
Report CancelUsing javascript you can do, but it is recommended to use Jquery to reduce the code.
For instance using Javascript :
function findMatches(){
var matches = [];
var searchEles = document.getElementsByClassName(“parent”)[0].children;
for(var i = 0; i < searchEles.length; i++) {
if(searchEles[i].tagName == ‘P’) {
if(searchEles[i].getAttribute(‘class’) == ‘child’) {
matches.push(searchEles[i]);
}
}
}
}
In this case, all the matches corresponding to the parent element will be returned.
When in Jquery, your function will be like this :
function findMatches(){
return $(‘.parent’).find(‘.child’);
}
I would personally recommend to use jquery for these operations because of lesser code and compressed javascript output on browser.