Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    Wildcard in getElementByID, possibile?

    Ciao a tutti,

    Mi servirebbe di catturare tutti gli elementi con id= ad esempio "lista_n" dove per n = un numero variabile.

    È possibile ?

    Grazie
    SGr33n

  2. #2
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Pagina senza titolo</title>
    
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    
    function Button1_onclick() 
    {
        var a = elementi_lista();
        for(var i = 0; i < a.length; i++)
        {
                alert(a[i].value);
        }
    }
    
    function elementi_lista()
    {
        var a = [];
        var es = document.getElementsByTagName("*");
        for(var i = 0; i < es.length; i++)
        {
            if(es[i].id && es[i].id.substr(0, 6) == "lista_")
                a.push(es[i]);
        }
        return a;
    }
    
    // ]]>
    </script>
    </head>
    <body>
        <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
    
        <input type="text" id="lista_0" value="valore_0" />
    
        <input type="text" id="lista_1" value="valore_1" />
    
        <input type="text" id="lista_2" value="valore_2" />
    
        <input type="text" id="lista_3" value="valore_3" />
    
    </body>
    </html>
    Pietro

  3. #3
    Grazie 1000 pietro
    SGr33n

  4. #4
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    visto che ci sono, ho recuperato delle funzioni che ho in libreria, più generali e forse più utili.

    metto la pagina
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Pagina senza titolo</title>
    
    <script language="javascript" type="text/javascript">
    // <!CDATA[
    
    function Button1_onclick() 
    {
        //var a = elementi_lista();
        var a = getElements("body", "input", "text", "lista_");
        for(var i = 0; i < a.length; i++)
        {
                alert(a[i].value);
        }
    }
    
    function elementi_lista()
    {
        var a = [];
        var es = document.getElementsByTagName("*");
        for(var i = 0; i < es.length; i++)
        {
            if(es[i].id && es[i].id.substr(0, 6) == "lista_")
                a.push(es[i]);
        }
        return a;
    }
    
    
    /*--------------------------------------------------------------------------
    trova gli elementi che soddisfano il criterio in un vettore:
    source:		contenitore o id contenitore. se null = body
    tagName:	se null = "*"
    type:		se null non ne tengo conto
    idSearch:	se valorizzato, tutti gli elementi con id che inizia con idSearch
    ----------------------------------------------------------------------------*/
    function getElements(source, tagName, type, idSearch)
    {
    	var v = [];
    	
    	var result = getTypeParameter(source);
    	if(result == 0)
    		source = document.body;
    	else if(result == 2)
    		source = document.getElementById(source);
    	else if(result == 4)
    		source = source;
    	else
    		return v;
    
    	tagName = (tagName == undefined)? "*" : tagName;
    	type = (type == undefined)? "" : type;
    	idSearch = (idSearch == undefined)? "" : idSearch;
    
    	var elementi = source.getElementsByTagName(tagName);
    	var n = elementi.length;
    	for(var i = 0; i < n; i++)
    	{
    		var elemento = elementi[i];
    		if( (elemento.type != undefined) && ((elemento.type.toLowerCase() == type.toLowerCase()) || (type == "")) )
    		{
    			if( (elemento.id != undefined) && (elemento.id.indexOf(idSearch, 0) == 0 || idSearch == "") )
    			{
    				v[v.length] = elemento;
    			}
    		}
    	}
    	
    	return v;
    
    
    }
    
    /*----------------------------------------------------------
    Restituisce il tipo di parametro ricevuto
    0 -> indefinito
    1 -> funzione
    2 -> id oggetto che supporta innerHTML
    3 -> id oggetto che supporta value
    4 -> oggetto che supporta innerHTML
    5 -> oggetto che supporta value
    6 -> vettore
    -----------------------------------------------------------*/
    function getTypeParameter(v)
    {
    	if(typeof(v) == "undefined")
    	{
    		//alert("undefined");
    		return 0;
    	}
    	else if(typeof(v) == "function")
    	{
    		//alert("funzione");
    		return 1;
    	}
    	else if(typeof(v) == "string")
    	{
    		if(document.getElementById(v) != undefined)
    		{
    			if(document.getElementById(v).value != undefined)
    			{
    				//alert(" id value");
    				return 3;
    			}
    			else if(document.getElementById(v).innerHTML != undefined)
    			{
    				//alert("id innerHTML");
    				return 2;
    			}
    		}
    		else return 0;
    		
    
    	}
    	
    	else if(typeof(v) == "object")
    	{
    		if(v == null)
    		{
    			//alert("null");
    			return 0;
    		}
    		else
    		{
    			if(v.length != undefined)
    			{
    				//alert("vettore");
    				return 6;
    			}
    			else
    			{
    				if(v.value != undefined)
    				{
    					//alert(" oggetto value");
    					return 5;
    				}
    				else if(v.innerHTML != undefined)
    				{
    					//alert("oggetto innerHTML");
    					return 4;
    				}
    				
    			}
    		}
    	}
    	//indefinito o altro
    	return 0;
    	
    }
    // ]]>
    </script>
    </head>
    <body>
        <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
    
        <input type="text" id="lista_0" value="valore_0" />
    
        <input type="text" id="lista_1" value="valore_1" />
    
        <input type="text" id="lista_2" value="valore_2" />
    
        <input type="text" id="lista_3" value="valore_3" />
    
    </body>
    </html>
    Pietro

  5. #5
    Grazie 1000 pietro,

    Funziona alla perfezione ho ordinato così:

    codice:
    function getElementsByWCID(WCID, tag) {
    
    	// dichiaro l'array
    	var a = [];
    
    	// recupero gli elementi
        var es = document.getElementsByTagName(tag);
        
    	// avvio il ciclo
    	for(var i = 0; i < es.length; i++) {
            if(es[i].id && es[i].id.substr(0, 6) == WCID)
                a.push(es[i]);
        }
    
    	// restituisco il risultato
        return a;
    }
    
    function countElements(WCID, tag) {
    
    	// recupero gli elementi
        var a = getElementsByWCID(WCID, tag);
    
    	// memorizzo il totale
    	var aElements = a.length;
    
    	// salvo il risultato
    	return aElements;
    }
    Che ne pensi?
    mi potresti spiegare quel .push ?
    SGr33n

  6. #6
    Utente di HTML.it L'avatar di pietro09
    Registrato dal
    Jan 2002
    Messaggi
    10,116
    Originariamente inviato da SGr33n

    Che ne pensi?
    mi potresti spiegare quel .push ?

    penso che se funziona va bene

    push aggiunge un elemento al vettore, trattato in questo caso come uno stack:

    push aggiunge, pop lo toglie
    Pietro

  7. #7
    Perfetto
    grazie ancora.
    SGr33n

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.