user1814374
user1814374

Reputation: 59

Defining a click event programmatically

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)
                    })

Upvotes: 1

Views: 97

Answers (2)

karim79
karim79

Reputation: 342695

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

Demo.

Upvotes: 3

thecodeparadox
thecodeparadox

Reputation: 87083

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

DEMO

Upvotes: 6

Related Questions