[JS] Problem z węzłem

0

Witam, mam taki kod w JS :

    var uchwyt= document.getElementById("new_input_1");
    element= document.createElement("input");		
		
    element.setAttribute("name", "all_value_txt");
    element.setAttribute("id", "all_value_txt");
    element.style.border='1px solid red';
    element.setAttribute("type", "text");				
					    
    uchwyt.appendChild(element);	

gdzies w bloku body jest

Problem w tym ze czepia sie uchwytu "new_input_1 has no properties"
Nie rozumiem tego przeciez istnieje w kodzie. Jak to rozwiazac ?

0

Czy ten kod js wywołujesz już po całkowitym załadowaniu sie strony? Poniżej działający przyklad.

<html>
<head>
<script type="text/javascript">
function f() {
	var uchwyt= document.getElementById("new_input_1");
    element= document.createElement("input");               
               
    element.setAttribute("name", "all_value_txt");
    element.setAttribute("id", "all_value_txt");
    element.style.border='1px solid red';
    element.setAttribute("type", "text");                               
                                           
    uchwyt.appendChild(element);         
}
window.onload=f;
</script>
</head>
<body>
<table>
<tr><td id="new_input_1"></td></tr>
</table>
</body>


</html>

1 użytkowników online, w tym zalogowanych: 0, gości: 1