1

I am creating a dynamic anchor tag like this

 var anch = $('<a />', {
                        'href': '#' + ctrlid,
                        'text': text
                    })

How do I add a click event while creating this tag that automatically calls a function say funcOne(ctrlid) and passes the ctrlid?

I tried this but no luck

 var anch = $('<a />', {
                        'href': '#' + ctrlid,
                        'text': text,
                        'onclick': funcOne(ctrlid)
                    })

2 Answers 2

6
var anch = $('<a />', {
    'href': '#' + ctrlid,
    'text': text
}).click(function () {
    // click handler code
});

DEMO

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

Comments

3
var anch = $('<a />', {
    'href': '#' + text,
    'text': text,
     on: {
         click: function () {
            // do something
         },
         someOtherEvent: function () {
            // do something
         }
     }
});

Demo.

1 Comment

Brilliant, exactly what I wanted

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.