on Sun 20 Sep 2015, 22:11 by LGforum
In this instance, neither are better on their own. It depends on the situation; if you are going to be manipulating any of the other elements any further then the first option is better since you have everything you need in variables already. For a short and concise way of simply grabbing the text then the second is better. It's all relative really.
I want to share here am absolutely tiny function I use on a Avacweb and other places which prevents me having to do any of the above.
It's basically just a shortcut function for all the annoying getElementsBy functions.
Here's how I'd do it:
[ic]_.getby('strong', _.getby('.module', _.getby('#profile-advanced-right')[0])[0].innerText[/ic]
It may look a little confusing for this specific example, but ultimately the _.getby function has been a favourite of mine for a while.
[ic] _.getby('.classname') _.getby('#id') _.getby('tagname')[/ic]
I want to share here am absolutely tiny function I use on a Avacweb and other places which prevents me having to do any of the above.
- Code:
[panda=js]_.getby = function(selector, context) {
context = context || document;
switch(selector.charAt(0)) {
case '#' :
return context.getElementById( selector.substr(1) );
case '.' :
return context.getElementsByClassName( selector.substr(1) );
default :
return context.getElementsByTagName( selector );
}
};
It's basically just a shortcut function for all the annoying getElementsBy functions.
Here's how I'd do it:
[ic]_.getby('strong', _.getby('.module', _.getby('#profile-advanced-right')[0])[0].innerText[/ic]
It may look a little confusing for this specific example, but ultimately the _.getby function has been a favourite of mine for a while.
[ic] _.getby('.classname') _.getby('#id') _.getby('tagname')[/ic]