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.

3
  • You are calling the function B() or defining there ? Function within another function (nested function) may not supported by Java script. Commented Aug 10, 2012 at 9:23
  • 1
    @JDeveloper You can't say that! it totally works : Nested functions Commented Aug 10, 2012 at 9:25
  • Sorry i was not clear. That's why i have used May not. Commented Aug 10, 2012 at 9:36

3 Answers 3

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

};
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it like that:

aTag.onclick = function() {

    B();

}

or even shorter:

aTag.onclick = B;

Comments

0

If you just want to call function B try this:

aTag.onclick = function() {
  var a = new A();
  a.B();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.