1

I have an image at the top right corner of a page. on click of the image i have to display a div that looks like a table with many rows. The div has to be positioned exactly below the image.I have tried the below code snippet.

<div><a href="javascript:Test()"><img align=right src="Test.png" /></a></div>

sample code snippet for test function

Test(){
var hNode = document.createElement('div');
hNode.style.display = "block";
hNode.style.overflow="hidden";enter code here
hNode.style.position = "absolute"; 
hNode.style.width = "400px";
hNode.style.height = "30px";
hNode.style.border = "1px solid  #666666";
hNode .appendChild(document.createTextNode("Test"));
}

But the div does not show up. What is wrong with this snippet? Any help would be great..

later2013
  • 41
  • 3

4 Answers4

1

Maybe use an onclick="Test()" in your <a> tag

  • for completeness-sake... [there is debate as to best practices for triggering javascript in this way](http://stackoverflow.com/questions/245868/what-is-the-difference-between-the-different-methods-of-putting-javascript-code) – Pedro del Sol Jan 22 '13 at 09:26
0

first : change a to <a href="javascript:void(0)" onclick="Test();">link</a>

second : in function implementation add function keyword before Test()

Mahmoud Farahat
  • 5,364
  • 4
  • 43
  • 59
0
 <div onclick="Test()"><img align=right src="Test.png" /></div> 

may help.

Nils
  • 806
  • 1
  • 9
  • 24
0
function Test(){
   var hNode = document.createElement('div');
   hNode.style.display = "block";
   hNode.style.overflow ="hidden";
   hNode.style.position = "absolute";
   hNode.style.width = "400px";
   hNode.style.height = "30px";
   hNode.style.border = "1px solid #666666";
   var t = document.createTextNode("Test");
   hNode.appendChild(t)
   document.body.appendChild(hNode);
 }

while all the other answers are addressing problems with your code the answer as to why it doesn't appear is you aren't actually appending the newly created div to document.body so although it exists it isn't part of the dom.

you can call this function using any of the methods described in other answers; i prefer something along the lines of

 <a href="javascript_policy.html" onclick="myFunction(); return false;">my link</a>

to direct users without javascript enabled to a page telling them why javascript is necessary for that particular link.

Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52