Egr. fcaldera,
lei mi precisava:
x è il riferimento al nodo il cui id è uguale a "myForm"
per esserne sicuro prova a stampare in output altre proprietà ad es.
,
e poi suggeriva, tenendo conto del fatto che x è il riferimento al nodo il cui id è uguale a "myForm", di provare a stampare in output altre proprietà ad es.:
codice:
var x=document.getElementById("myForm");
...
document.write(x);
document.write(x.id); // restituisce "myForm"
document.write(x.tagName); // restituisce "FORM"
document.write(x.parentNode.tagName); // restituisce "BODY"
Ho seguito i suoi suggerimenti, ed è bingo!
Questo il codice:
Codice PHP:
<html>
<body>
<form id="myForm" action="http://www.google.com" method="POST">
Firstname: <input id="fname" type="text" value="Mickey" />
Lastname: <input id="lname" type="text" value="Mouse" />
<input id="sub" type="button" value="Submit" />
</form>
Get x in the form:
<script type="text/javascript">
var x=document.getElementById("myForm");
document.write("
");
document.write(x);
var x1=document.getElementsByTagName("myForm");
document.write("
");
document.write(x1);
var x=document.getElementById("myForm").tagName;
document.write("
");
document.write(x);
var x=document.getElementById("myForm").parentNode.tagName;
document.write("
");
document.write(x);
var x=document.getElementById("myForm").lenght;
document.write("
");
document.write(x);
var x=document.getElementById("myForm").method;
document.write("
");
document.write(x);
var x=document.getElementById("myForm").name;
document.write("
");
document.write(x);
var x=document.getElementById("myForm").action;
document.write("
");
document.write(x);
</script></p>
Get first element in the form:
<script type="text/javascript">
var x=document.getElementById("myForm");
document.write("
");
document.write(x.elements[0].value);
</script></p>
Get second element in the form:
<script type="text/javascript">
var x=document.getElementById("myForm");
document.write("
");
document.write(x.elements[1].value);
</script></p>
Get third element in the form:
<script type="text/javascript">
var x=document.getElementById("myForm");
document.write("
");
document.write(x.elements[2].value);
document.write("
");
</script></p>
Get all elements in the form:
<script type="text/javascript">
var x=document.getElementById("myForm");
document.write("
");
for (var i=0;i<x.length;i++)
{
document.write(x.elements[i].value);
document.write("
");
}
</script></p>
</body>
</html>
[/i]
e questo è l'output:
Codice PHP:
Firstname: Mickey Lastname: Mouse
Get x in the form:
[object]
[object]
FORM
BODY
undefined
post
[url]http://www.google.com[/url]
Get first element in the form:
Mickey
Get second element in the form:
Mouse
Get third element in the form:
Submit
Get all elements in the form:
Mickey
Mouse
Submit
La ringrazio per avermi dato lo spunto per approfondire alcuni aspetti del DOM che reputo fondamentale, e cioè:
- DOM (Document Object Model), modello che descrive come sono interconnessi i diversi oggetti di una pagina.
- Si accede al DOM con Javascript.
- Abbiamo a disposizione un insieme di funzioni, metodi e proprietà che fanno un grosso lavoro per noi.
P.S.N°1 MI SCUSI L'IGNORANZA, MA COME SI FA UNO SNAPSHOT (... e dire che lo sapevo fare, ma ora me lo sono scordato!). A ME INTERESSA VISUALIZZARE ANCHE LE IMMAGINI DELL'OUTPUT DI UN CODICE. LAVORO CON WINDOWS XP E I.E.
grazie.grazie. GRAZIE!
P.S.N°2: approfitto del fatto che lei è un [I]moderatore per chiederle: posso postare in HTML.it forum > Lato client > JavaScript > [DOM/Javscript: come manipolare object?] anche problemi relativi ad Ajax? Il fatto è che non ho trovato una sezione specifica su HTML.it forum , e ... in base all'acronimo, questo SAREBBE IL POSTO GIUSTO. Alternativa possibile:XML! Ma, onestamente, se fosse possibile ed in linea di cortesia, vorrei postare quì alcuni dubbi e problemi su AJAX.
Cordiali saluti
Cicerone80