Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - how to pass value into javascript code

I have been having a lot of trouble trying to get value into javascript and pass it to alert. Here is the code that I use to pass the value....

        <img onclick="handpost()" id="colorface" name="red" src="http://www.asl-ela.org/image/redface.png" alt="Red face" /></a>

Then I use function to get the data from img tag....

<script type="text/javascript">
   function handpost()  {
      var color = document.getElementById('colorface').name;
      alert(color);
   }
</script>

I keep getting alert box saying undefined. What does that mean?!?!?!

like image 298
Harrison F Parson Jr Avatar asked Jul 28 '26 01:07

Harrison F Parson Jr


1 Answers

It means exactly what it reads: document.getElementById('colorface').name is undefined. document.getElementById('colorface') may exist but it doesn't have property name. Try document.getElementById('colorface').getAttribute('name').
name is not standard attribute of img but if you add it then you can access it. enter image description here

like image 94
Alex Kudryashev Avatar answered Jul 29 '26 14:07

Alex Kudryashev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!