I wrote a JavaScript function that will return a "list" of elements that has Id that start with some value:
function getElementsWithIdPrefix(prefix){
if (document.querySelectorAll){
return document.querySelectorAll('*[id^="' + prefix + '"]');
} else {
// none modern browsers support
var elements = document.getElementsByTagName('*');
var relevantElements = [];
for (var i = 0; i < elements.length; i++){
if (element.id && element.id.indexOf(prefix) !== -1){
relevantElements.push(element);
}
}
return relevantElements;
}
}
As you can see my function will return different types depends on browser support for document.querySelectorAll
and I have two question regarding this:
- Is this bad? I mean - In JavaScript typing system does it consider as a code smell or bad practice?
- How can I create Node objects for each Element by my self and construct a new NodeList containing these elements to return?
Aucun commentaire:
Enregistrer un commentaire