Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279890

Javascript - Change button onclick to an existing function

I have a button:

<button onclick = "doDisactivate(5);" id = "status_button_5" value = "Disactivate" />

where 5 is a dynamically added ID. I have two javascript functions, doDisactivate(ID) and doActivate(5).

function doDisactivate(serviceID) {
    // logic that changes the button onclick to doActivate(serviceID)
}

function doActivate(serviceID) {
    // logic that changes the button onclick to doActivate(serviceID)
}

I'm guessing I can do the following with jquery:

${"#status_button_" + serviceID}.click(function() {
    doActivate(serviceID);
});

But is there a way to directly assign the function while passing the ID as a parameter?

Upvotes: 1

Views: 1287

Answers (2)

Waqleh
Waqleh

Reputation: 10161

do something like this

${"#status_button_" + serviceID}.click(function(event) {
    doActivate(event.target.id);
});

or

onClick="doDisactivate(this.id)"

Upvotes: 1

rdiazv
rdiazv

Reputation: 1153

As you aren't using jquery event bindings, you could use $("#status_button_5").attr("onclick", "doActivate(5);") to directly change the onclick attribute.

Upvotes: 2

Related Questions