Ho una funzione javascript a cui passo un parametro. Il parametro rappresenta l'id di un elemento (un campo nascosto) nella mia pagina web. Voglio cambiare il valore di questo elemento.
function myFunc(variable){
var s= document.getElementById(variable);
s.value = 'New value'
}
Quando lo faccio, ottengo un errore che il valore non può essere impostato perché l'oggetto è nullo. Ma so che l'oggetto non è nullo perché lo vedo nel codice html generato dal browser. Comunque, ho provato il seguente codice per il debug
function myFunc(variable){
var x = variable;
var y = 'This-is-the-real-id'
alert(x + ', ' + y)
var s= document.getElementById(x);
s.value = 'New value'
}
Quando appare il messaggio di avviso, entrambi i parametri sono gli stessi, ma ottengo ancora l'errore. Ma tutto funziona bene quando faccio
var s= document.getElementById('This-is-the-real-id');
s.value = 'New value'
Come posso risolvere questo problema per favore
EDIT
L'elemento per il quale sto impostando il valore è un campo nascosto e l'id viene rilevato dinamicamente al caricamento della pagina. Ho provato ad aggiungere questo nella funzione $(document).ready ma non ha funzionato
Se myFunc(variabile) viene eseguito prima che la textarea sia resa nella pagina, otterrete un errore di eccezione nullo.
<html>
<head>
<title>index</title>
<script type="text/javascript">
function myFunc(variable){
var s = document.getElementById(variable);
s.value = "new value";
}
myFunc("id1");
</script>
</head>
<body>
<textarea id="id1"></textarea>
</body>
</html>
//Error message: Cannot set property 'value' of null
Quindi, assicurati che la tua textarea esista nella pagina, e poi chiama myFunc, puoi usare window.onload o la funzione $(document).ready. Spero che sia utile.
Dato
<div id="This-is-the-real-id"></div>
allora
function setText(id,newvalue) {
var s= document.getElementById(id);
s.innerHTML = newvalue;
}
window.onload=function() { // or window.addEventListener("load",function() {
setText("This-is-the-real-id","Hello there");
}
farà quello che vuoi
Dato
<input id="This-is-the-real-id" type="text" value="">
allora
function setValue(id,newvalue) {
var s= document.getElementById(id);
s.value = newvalue;
}
window.onload=function() {
setValue("This-is-the-real-id","Hello there");
}
farà quello che vuoi
function setContent(id, newvalue) {
var s = document.getElementById(id);
if (s.tagName.toUpperCase()==="INPUT") s.value = newvalue;
else s.innerHTML = newvalue;
}
window.addEventListener("load", function() {
setContent("This-is-the-real-id-div", "Hello there");
setContent("This-is-the-real-id-input", "Hello there");
})
<div id="This-is-the-real-id-div"></div>
<input id="This-is-the-real-id-input" type="text" value="">
prova come sotto, funzionerà...
<html>
<head>
<script>
function displayResult(element)
{
document.getElementById(element).value = 'hi';
}
</script>
</head>
<body>
<textarea id="myTextarea" cols="20">
BYE
</textarea>
<br>
<button type="button" onclick="displayResult('myTextarea')">Change</button>
</body>
</html>