1

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?

2 Answers 2

2

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

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

2 Comments

Are event bindings better? What do they add?
It's just easier to work with. For example, you could do something like var activated = false; $("#status_button_5").on("click", function(){ if(activated){doDisactivate(5);} else {doActivate(5);} activated = !activated; }).
1

do something like this

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

or

onClick="doDisactivate(this.id)"

2 Comments

This works, but I want the function directly, not through another anonymous function.
you can add the event parameter to you doActivate function, and within the function, call event.target.id.

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.