Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change the position of image on click

   <html>
   <head>
   <script type="text/javascript">
   function changepic()
   {
   document.getElementById("demo").style.top=2000;  
   }
   </script>
    </head>
   <body>

 <h1>My First Web Page</h1>
  <div id="demo">
  <img src="./omna.jpg"  style="position:absolute; left: 400; top: 100; width: 200;      height: 200;"/>
  </div>
  <button type="button" onclick="changepic()">change pic</button>
  </body>
  </html>

Why im unable to change the position of picture whats going wrong ?

<img id="demo"> ' google says it works

I dont know but the above html page isnt working I wanted to change the position of an image when I click the button, please help me with these. Im using google chrome

<img onclick="somefunc()"> can I set function onclick to image also? right?

But its not working please help me with the html Code!

like image 603
niko Avatar asked Sep 20 '25 19:09

niko


1 Answers

You're trying to set the top of an element with the id demo, which is a <div>. I assume that div doesn't have position:absolute set on it so won't move anywhere, and I guess you want the <img> to move and and not the <div> anyway. If you remove the id from the <div> and add it to the <img> (like in your second piece of code above) you shouldn't have any problems. This code should work:

<html>
<head>
<script type="text/javascript">
function changepic()
{
 document.getElementById("demo").style.top=2000 + "px";  
}
</script>
</head>
<body>

<h1>My First Web Page</h1>
<div>
<img id="demp" src="./omna.jpg"  style="position:absolute; left: 400; top: 100; width: 200;      height: 200;"/>
</div>
<button type="button" onclick="changepic()">change pic</button>
</body>
</html>
like image 174
Clive Avatar answered Sep 22 '25 10:09

Clive