byTag: shortcut for getElementByTagName

With this function we shortened the code and make it easier to read, optionally we can do that instead of an array with all the elements with the specified tag we return one object in the position of the array specified by the third optional parameter


	var objs=gEle(obj).getElementsByTagName(tagName);
	if (num==undefined)	return objs;
	if (objs.length>=num) return objs[num];
	return objs;


obj (string with the id of the object or object)
Can be an object (document, jQuery(".anothing")[0] etc) or a string with the id of the object that we want to get the objects with the tag specified in the following parameter
tagName (string)
Text string with any tag: "p", 'pre', "script"
num (integer) Optional
In the case that this value is passed, it will return the object with that position in the array. Being zero based to get the first element of the array we must pass 0, the second 1, etc

Returned value

Array of objects with the especified tag or one only object if the third optional parameter is provided


Change style to the 1st “pre” object inside the object with id “primary”
p=byTag(gEle("primary"),"pre",1);"dashed 3px #f0f";
Add border to the 1st object with tag “p” in the document
In this sample we use the third optional parameter for get an object instead an array
p=byTag(document,"p",0);"3px solid #ff0";
Add border to all the objects with tag “p” in the document
var p=byTag(document,"p");
for (var n=0;n<p.length;n++){
	p[n].style.border='solid 3px #f00';

Leave a Reply

Your email address will not be published. Required fields are marked *