Skip to content
Advertisement

Uncaught TypeError: Cannot set property ‘src’ of null javascript

i have little problem when i want to insert image into form, the code work on other form but in this form it not work with Uncaught TypeError: Cannot set property ‘src’ of null
here is my code
javascript :

function edit(ids,idserv,namaproj,linkproj,img){
//alert(ids+" "+idserv+" "+namaproj+" "+linkproj+" "+img);
document.getElementById('ids').value=ids;
document.getElementById('ns').value=namaproj;
document.getElementById('kt').value=linkproj;
document.getElementById('link').value=idserv;
document.getElementById('img1').src="./img/"+img;

document.getElementById("myFormedit").style.display = "block";}

form :

   <form action="tservices" method = "POST" class="form-container" enctype="multipart/form-data">
<h1>Edit Services</h1>
<input type="text" id="ids" name="ids" hidden>
<label><b>Nama Project</b></label>
<input type="text" name = "nama" id="ns"  required>
<label>Link Project</label>
<input type="text" name = "ket" id="kt" required>
    <label><b>Service</b></label>
<select name="tujuan" id="link">
<?php
$sartikel = mysqli_query($conn,"select * from services");
$nartikel = mysqli_num_rows($sartikel);
for($i=1;$i<=$nartikel;$i++){
    $r = mysqli_fetch_array($sartikel);
    $id = $r[0];
    $judul = $r[1];
    $isi = $r[2];
    $tgl = $r[3];
    $gmbr = $r[4];
    $st = $r[5];
?>
<option value="<?php echo $id ?>"><?php echo $judul; ?></option>
<?php 
}
?>
<h3>Foto Project</h3>
<img id="img1" height="150" width="150" ><br>
<input class="w3-input" type="file" accept="image/*" onchange="loadFile2(event)" name="fd">

</select>
<button type="submit" class="btn"name="button" value="edit">Ubah</button>
<button type="button" class="btn cancel" onclick="closeFormedit()">Tutup</button>

Advertisement

Answer

Your img tag is inside the select tag so your javascript is not recognizing the element with id ‘img’ and showing and the TypeError.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement