php - Javascript Error "Null is not an Object" with a dynamically created html table -
i have dynanamically created html table picture in last cell of each row (like.png). achieve after user has clicked on picture 1 diplayed (like1.png). keep getting "null not object", there maybe wrong javascript code ...
thank :)
here php creates table :
<?php $pictureid = 1; while($results = mysql_fetch_array($raw_results)){ // $results = mysql_fetch_array($raw_results) puts data database array, while it's valid loop echo '<tr>'; echo'<td>'. ucfirst($results['song_name']).'</td>'; echo'<td>'. ucfirst($results['song_artist']).'</td>'; //echo'<td>'. ucfirst($results['song_album']).'</td>'; echo '<td>'; echo '<img src="images/like.png" id="artist'.$pictureid.'" onclick="action(artist'.$pictureid.')"/></a>'; echo '</td>'; echo '</tr>'; $pictureid = $pictureid + 1; }
and here javascript :
<script language="javascript"> function action(imageid) { if(document.getelementbyid(imageid).src == 'like.png' ) document.getelementbyid(imageid).src = 'like1.png'; else document.getelementbyid(imageid).src = 'like1.png'; } </script>
you're missing quotes here:
echo '<img ... onclick="action(\'artist'.$pictureid.'\')"/></a>'; // quotes missing here ----------^---------------------^
in output html should like, e.g., this:
<img ... onclick="action(artist1)"/>
this call method action()
, use variable of name artist1
, not exists. in method document.getelementbyid()
returns null
, error.
your method, however, requires string input , should enclose parameter quotes, generates output this:
<img ... onclick="action('artist1')"/>
Comments
Post a Comment