Martin Szyszlican wrote:
> Hola, hace un tiempo que tenía ganas de suscribirme aqui.
> Retomé en estos días el JavaScript, estoy haciendo una serie de
> utilidades para mi CMS.
> Una de ellas requiere que encuentre un elemento en un array por una de
> sus propiedades.
> Es decir, yo sé que en un array hay un elemento que tiene una propiedad
> "id" con determinado valor.
> Hice un script (copiando de otros lados) que pretende hacer eso, pero no
> parece funcionar.
> Aquí el código:
>
> Array.prototype.findElementById = function( element_id ) {
> for( a in this ) {
> if (this[a].id) {
> if( this[a].id == element_id ) {
> return this[a];
> }
> else if( this[a] instanceof Array) {
> return this[a].findElementById( element_id );
> }
> }
> }
> return false;
> }
>
hola!
bueno, a parte de que si se debe utilizar un prototype o no sobre un
array de objetos :P he probado el siguiente codigo y funciona sin
problemas, asi que debe ser mas bien un problema al rellenar el array :)
[code]
<html>
<head>
<title>prueba</title>
<script>
Array.prototype.findElementById = function( element_id ) {
for( a in this ) {
if (this[a].id) {
if( this[a].id == element_id ) {
return this[a];
}
else if( this[a] instanceof Array) {
return this[a].findElementById( element_id );
}
}
}
return false;
}
</script>
</head>
<body>
<div id="uno">1</div>
<div id="dos">2</div>
<div id="tres">3</div>
<div id="cuatro">4</div>
<div id="cinco">5</div>
<script>
var aObj = Array();
aObj.push(document.getElementById('uno'));
aObj.push(document.getElementById('dos'));
aObj.push(document.getElementById('tres'));
aObj.push(document.getElementById('cuatro'));
aObj.push(document.getElementById('cinco'));
alert(aObj.findElementById('tres'));
alert(aObj.findElementById('seis'));
</script>
</body>
</html>
[/code]
un saludo :)
_______________________________________________
javaEScript mailing list
[email protected]
http://lists.scriptia.net/listinfo.cgi/javaescript-scriptia.net