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
CodebyTag=function(obj,tagName,num){
var objs=gEle(obj).getElementsByTagName(tagName);
if (num==undefined) return objs;
if (objs.length>=num) return objs[num];
return objs;
}
Parameters
- 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