0

How to call a function defined in same function on click of hyperlink

Function A()
{
    var mydiv = document.getElementById("myDiv"); 
    var aTag = document.createElement('a');  
    aTag.setAttribute('href',"yourlink.htm");   
    aTag.innerHTML = "link text"; 
    mydiv.innerHTML=""; 
    mydiv.innerHTML=aTag; 

    Function B()
    {
        // do stuff ---           
    }    
}

On click of hyperlink aTag i need to call functionB. Please suggest.

Faust
  • 15,130
  • 9
  • 54
  • 111
pavan
  • 451
  • 1
  • 5
  • 16
  • You are calling the function B() or defining there ? Function within another function (nested function) may not supported by Java script. – JDGuide Aug 10 '12 at 09:23
  • 1
    @JDeveloper You can't say that! it totally works : [Nested functions](http://stackoverflow.com/questions/7295634/javascript-nested-function) – Vishal Aug 10 '12 at 09:25
  • Sorry i was not clear. That's why i have used May not. – JDGuide Aug 10 '12 at 09:36

3 Answers3

2
aTag.onclick = function() {
  //...

};
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

You can do it like that:

aTag.onclick = function() {

    B();

}

or even shorter:

aTag.onclick = B;
Amberlamps
  • 39,180
  • 5
  • 43
  • 53
0

If you just want to call function B try this:

aTag.onclick = function() {
  var a = new A();
  a.B();
}
Vishal
  • 1,236
  • 1
  • 9
  • 16