PDA

View Full Version : [Javascript] Piccolo aiuto sull'assegnazione di valori a una variabile


Hieicker
17-04-2013, 12:13
Ciao a tutti :) ! Sto provando a scrivere e sperimentare un po di javascript e mi sono bloccato su un piccolo problema. Penso dipenda dalla mia scarsa conoscenza del linguaggio :).
La premessa è che ho una pagina html con all'interno i seguenti due div.

<div id="progressbar">
<div id="inside_bar" name="bar" attributo1="..." attributo2="...">
</div>
</div>


Fin qua tutto ok. il problema sorge quando in uno script provo ad assegnare ad una variabile il valore dell'attributo name. Ho provato a scrivere il seguente codice (ho provato in due modi).


var test2 = document.getElementById("inside_bar").getAttribute("name");

var test2 = document.getElementById("inside_bar").getAttribute("name").value;


Tuttavia nella console di firefox quando provo a fare il log della variabile, mi restituisce sempre lo stesso errore, ovvero "TypeError: document.getElementById(...) is null".
Cosa sto sbagliando?

Grazie a tutti :)!

Edit: risolto!

simo8
18-04-2013, 12:04
a me il primo metodo funzione correttamente :

alert(document.getElementById("inside_bar").getAttribute("name"));

restituisce bar.

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.